【发布时间】:2014-04-10 06:48:51
【问题描述】:
我有这个代码:
private void btnPotvrdi_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=BCKPSRVR;Initial Catalog=protokol;Integrated Security=True");
con.Open();
SqlCommand sc = new SqlCommand("insert into Knjiga (Datum_prijema, Predmet, Posiljalac, Adresa_posiljaoca, Primaoc, Datum_proslijeda) values ('" + dateTimePicker1.Value.ToString("yyyy-MM-dd") + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + dateTimePicker2.Value.ToString("yyyy-MM-dd") + "');", con);
int o=sc.ExecuteNonQuery();
MessageBox.Show(o + " :Podaci su unešeni");
con.Close();
}
当我插入此表时,我有一个名为 Osnovni_br 的 auto_increment int 主键列。如何在 MessageBox 或 MessageBox 中获取该列 (Osnovni_br) 的最后一个值?
【问题讨论】:
-
听说过 SQL 注入攻击吗?
-
谨防Sql注入。您可以将
SELECT SCOPE_IDENTITY();附加到您的查询并使用ExecuteScalar(),注意它返回一个十进制值。我无缘无故地假设MSSQL。 -
@Silvermind 你能为我的代码做一个例子吗?
标签: c# auto-increment messagebox submit-button