主题:[讨论]向数据库插入数据,执行后怎么没反应?
private void insertbtn_Click(object sender, System.EventArgs e)
{
SqlConnection myConnection = new SqlConnection("user id=sa;password=qazwsx;initial catalog=test;data source=FZKX-116;");
myConnection.Open();
String insertCmd = "insert into test1( IDTB,FirstNameTB,LastNameTB)values(@Id,@FName,@LName) ";
SqlCommand myCommand = new SqlCommand(insertCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.Char, 10));
myCommand.Parameters["@Id"].Value = TB1.Text;
myCommand.Parameters.Add(new SqlParameter("@FName", SqlDbType.Char, 10));
myCommand.Parameters["@FName"].Value = TB2.Text;
myCommand.Parameters.Add(new SqlParameter("@LName", SqlDbType.Char, 10));
myCommand.Parameters["@LName"].Value = TB3.Text;
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
Info.Text = "更新成功!";
}
{
SqlConnection myConnection = new SqlConnection("user id=sa;password=qazwsx;initial catalog=test;data source=FZKX-116;");
myConnection.Open();
String insertCmd = "insert into test1( IDTB,FirstNameTB,LastNameTB)values(@Id,@FName,@LName) ";
SqlCommand myCommand = new SqlCommand(insertCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.Char, 10));
myCommand.Parameters["@Id"].Value = TB1.Text;
myCommand.Parameters.Add(new SqlParameter("@FName", SqlDbType.Char, 10));
myCommand.Parameters["@FName"].Value = TB2.Text;
myCommand.Parameters.Add(new SqlParameter("@LName", SqlDbType.Char, 10));
myCommand.Parameters["@LName"].Value = TB3.Text;
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
Info.Text = "更新成功!";
}