WebForm1.aspx.cs
1
2
3
4
5
6
7
8
|
protected
void
Button1_Click(
object
sender, EventArgs e)
{
Users user =
new
Users();
user.UserID = TextBox1.Text;
user.UserName = TextBox2.Text;
user.UserAge = TextBox3.Text;
user.InsertByProc();
}
|
Users.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
public
class
Users
{
public
string
UserID;
public
string
UserName;
public
string
UserAge;
public
bool
InsertByProc()
{
SqlParameter[] Params =
new
SqlParameter[3];
Params[0] =
new
SqlParameter(
"@UserID"
, SqlDbType.VarChar, 50);
Params[1] =
new
SqlParameter(
"@UserName"
, SqlDbType.VarChar, 50);
Params[2] =
new
SqlParameter(
"@UserAge"
, SqlDbType.VarChar, 50);
Params[0].Value = UserID;
Params[1].Value = UserName;
Params[2].Value = UserAge;
UseProc(
"Proc_UsersAdd"
, Params);
return
true
;
}
public
void
UseProc(
string
ProcName, SqlParameter[] Prams)
{
string
ConStr = ConfigurationManager.ConnectionStrings[
"strCon"
].ConnectionString;
SqlConnection con =
new
SqlConnection(ConStr);
con.Open();
SqlCommand cmd =
new
SqlCommand(ProcName, con);
cmd.CommandType = CommandType.StoredProcedure;
if
(Prams !=
null
)
{
foreach
(SqlParameter Parameter
in
Prams)
{
cmd.Parameters.Add(Parameter);
}
}
cmd.ExecuteNonQuery();
con.Close();
}
}
|
备注:SqlParameter中的变量名(即@UserID,@UserName和@UserAge),必需与存储过程中的变量名称相同。
如何在SQL Server中创建存储过程,参考链接:
http://www.cnblogs.com/sosoft/p/3535696.html
本文转自daniel8294 51CTO博客,原文链接:http://blog.51cto.com/acadia627/1903754,如需转载请自行联系原作者