110,535
社区成员
发帖
与我相关
我的任务
分享
private void button1_Click(object sender, EventArgs e)
{
string sql = "insert into table values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
int i = ReturnNonQuery(sql,false);
if (i > 0)
{
MessageBox.Show("插入成功");
}
else
{
MessageBox.Show("插入失败");
}
}
public int ReturnNonQuery(string sql, bool isproc, params SqlParameter[] p)
{
string constr = @"server=.;database=你的库;uid=sa;pwd=sa";
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(sql, con);
//如果是存储过程,执行 否者 执行语句
int j = 0;
if (isproc == true)
{
cmd.CommandType = CommandType.StoredProcedure;
}
for (int i = 0; i < p.Length; i++)
{
cmd.Parameters.Add(p[i]);
}
try
{
con.Open();
j = cmd.ExecuteNonQuery();
return j;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Dispose();
}
}