基本上我所做的是,单击按钮后,程序将根据用户选择的内容从特定行中提取数据,并使用INSERT
. 以下是代码。
private void button3_Click(object sender, EventArgs e)
{
const String connectionString = "Data Source = Vanessa-PC\\SQLEXPRESS; Initial Catalog = IUMDC; Connect Timeout = 15; Integrated Security = true";
SqlConnection con = new SqlConnection(connectionString);
//int SituationID1;
label24.Show();
foreach (SitID x in Sittbl)
{
if (x.ID == Convert.ToInt16(comboBox1.SelectedItem))
{
try
{
con.Open();
SqlCommand command = new SqlCommand("SELECT * FROM Situation WHERE SituationID=" + x.SitIDs, con);
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
sitid1 = Convert.ToInt32(dr[0]);
name1 = dr[4].ToString();
incident1 = Convert.ToDateTime(dr[1]);
charges1 = dr[5].ToString();
nature1 = dr[2].ToString();
}
con.Close();
}
catch (SqlException ex)
{
MessageBox.Show("Database failed to connect" + ex.Message);
}
//SituationID = x.SitIDs;
}
}
try
{
con.Open();
SqlCommand command1 = new SqlCommand("INSERT INTO CurrentSit VALUES (" + sitid1 + ",'" + incident1.ToString("YYYY-mm-DD") + "', '" + nature1 + "', '" + name1 + "', '" + charges1 + "'", con);
SqlDataReader dr1 = command1.ExecuteReader();
con.Close();
}
catch (SqlException ex)
{
MessageBox.Show("Database failed to connect" + ex.Message);
}
//Situation Sit = new Situation();
//Sit.ShowDialog();
}
我的代码失败并说
“行项目附近的语法不正确”
两张表的类型相同,我已经尝试过彻底测试!