c# sql 另一个 SqlParameterCollection 中已包含 SqlParameter。
代码如下。protectedvoidButton1_Click(objectsender,EventArgse){inti;i=Insert(TextBox1.Text,...
代码如下。
protected void Button1_Click(object sender, EventArgs e)
{
int i;
i = Insert(TextBox1.Text, TextBox2.Text);
Label1.Text = i.ToString();
}
public int Insert(string name,string pwd)
{
SqlConnection conn = new SqlConnection("data source=127.0.0.1,1433;User ID=aaa;pwd=123456;Initial Catalog=aaa");
conn.Open();
string sql = "insert into users(name,pwd) values(@name,@pwd)";
SqlCommand cmd = new SqlCommand(sql,conn);
SqlParameter parn = new SqlParameter("@name",name);
cmd.Parameters.Add(parn);
SqlParameter parp = new SqlParameter("@pwd", pwd);
cmd.Parameters.Add(parn);
int result = cmd.ExecuteNonQuery();
//result接收受影响行数,也就是说result大于0的话表示添加成功
cmd.Parameters.Clear();
conn.Close();
cmd.Dispose();
return result;
} 展开
protected void Button1_Click(object sender, EventArgs e)
{
int i;
i = Insert(TextBox1.Text, TextBox2.Text);
Label1.Text = i.ToString();
}
public int Insert(string name,string pwd)
{
SqlConnection conn = new SqlConnection("data source=127.0.0.1,1433;User ID=aaa;pwd=123456;Initial Catalog=aaa");
conn.Open();
string sql = "insert into users(name,pwd) values(@name,@pwd)";
SqlCommand cmd = new SqlCommand(sql,conn);
SqlParameter parn = new SqlParameter("@name",name);
cmd.Parameters.Add(parn);
SqlParameter parp = new SqlParameter("@pwd", pwd);
cmd.Parameters.Add(parn);
int result = cmd.ExecuteNonQuery();
//result接收受影响行数,也就是说result大于0的话表示添加成功
cmd.Parameters.Clear();
conn.Close();
cmd.Dispose();
return result;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询