【发布时间】:2017-08-13 11:46:22
【问题描述】:
我正在制作一个表单,当该表单打开时,该表单中已经有一个 ID 显示。而且我只是以打开的形式更新它的列。但我的代码是在数据库中插入两个 ID。
这是我的代码。
private void btnAdd_Click(object sender, EventArgs e)
{
string insertSql =
"INSERT INTO Products(BrandName) OUTPUT INSERTED.ProductID VALUES(NULL)";
using (SqlConnection myConnection = new SqlConnection("Data Source=BENJOPC\\SQLEXPRESS;Initial Catalog=MARISCHELLdatabase;Integrated Security=True"))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
myCommand.ExecuteNonQuery();
Int32 newId = (Int32)myCommand.ExecuteScalar();
string aydi = newId.ToString();
myConnection.Close();
AddProducts ap = new AddProducts(aydi);
ap.FormClosing += new FormClosingEventHandler(this.AddProducts_FormClosing);
ap.ShowDialog();
pictureBox1.Image = null;
}
}
【问题讨论】: