c#插入数据失败

时间:2017-04-14 10:17:50

标签: c# forms visual-studio-2013

我的数据库似乎已连接,选择查询正在返回成功的解决方案,但是当我向表中添加数据时,它会成功执行,而数据不会出现在数据库中

private void button2_Click(object sender, EventArgs e)
{
    try
    {
        SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\DataHub.mdf;Integrated Security=True");
        conn.Open();

        SqlCommand comm = new SqlCommand("select count(*) from companies",conn);
        int count = (int)comm.ExecuteScalar();
        MessageBox.Show(count.ToString());
        comm = new SqlCommand("Insert into Companies Values ( " + (count+1) + ", '" + companyName.Text + "' , '" + openDate.Text + "' , '" + companyDesc.Text + "');", conn);
        comm.ExecuteNonQuery();
        MessageBox.Show(companyName.Text + " Successfully Created rows: ");
        conn.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

0 个答案:

没有答案