SqlException was unhandled

2019-08-21 02:29发布

I'm getting an similar error in the screenshot below. My question is; when I attempt to write to table those informations inside of textboxes,that's gives me error.Where I wrong? By the way,I'm using SQL Server Express 2008. enter image description here

private void btnprnsave_Click(object sender, EventArgs e)
    {          
        SqlConnection conn = DBConfigs.DataCon();
        SqlCommand record = new SqlCommand ("insert into dbo.PersonalInformationTable (PersonalName,PersonalSurname,PersonalEGN) values ("+txtPrnName.Text+","+txtPrnSurName.Text+","+txtPrnEgn.Text+")", conn);
        record.ExecuteNonQuery();
        conn.Close();
   }

3条回答
手持菜刀,她持情操
2楼-- · 2019-08-21 03:08

It's a bit hard to tell, but it looks like you need to escape your values in the insert statement.

"insert into dbo.PersonalInformationTable (PersonalName,PersonalSurname,PersonalEGN) values ('"+txtPrnName.Text+"','"+txtPrnSurName.Text+"','"+txtPrnEgn.Text+"')", conn);
查看更多
对你真心纯属浪费
3楼-- · 2019-08-21 03:11
  1. Format properly your SQL (single quotes around tesxtual values or use parameters)

  2. Chekc if the database file exists.

  3. Do not attach the database file at the same time with SQL Server Management Studio Express as you can't attach the same database file to twice to two different SQL Server (Express) instances.

  4. Always make sure you close the database connection. This shouls be achieved with the using statement as regardless how that block of code is left (exception or normal program flow) the Dispose() method will be called. This will always close the connection, and you have avoided a connection leak.

查看更多
Juvenile、少年°
4楼-- · 2019-08-21 03:21

You need to put single quotes around the textual values in your query. Even better; use parameters...
The exception you get might be caused by something else, but the query in itself is not correct, as stated above...

查看更多
登录 后发表回答