本文介绍了插入值到数据库表中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我尝试在sqlserver中的一个名为student的表中插入一个值,但我在行cmd.ExecuteNonQuery中得到错误; public partial class additem:System.Web.UI.Page { protected void Page_Load( object sender,EventArgs e) { } protected void TextBox1_TextChanged( object sender,EventArgs e) { } protected void addButton_Click( object sender,EventArgs e) { SqlConnection cn = new SqlConnection( @ Server = .\SQLEXPRESS; DataBase = first; Integrated Security = true; ); SqlCommand cmd; cmd = new SqlCommand( 插入student(id)值( + idTextBox.Text + ),cn) ; 尝试 { cn.Open(); cmd.ExecuteNonQuery; resultLabel.Text = 已成功添加; } catch (SqlException ex) { resultLabel.Text =( 错误 + ex.Message); } 最后 { cn.Close(); } } 解决方案 i try to insert a value into table called student in sqlserver but i get error in line cmd.ExecuteNonQuery; public partial class additem : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } protected void TextBox1_TextChanged(object sender, EventArgs e) { } protected void addButton_Click(object sender, EventArgs e) { SqlConnection cn = new SqlConnection(@"Server=.\SQLEXPRESS; DataBase=first; Integrated Security=true;"); SqlCommand cmd; cmd = new SqlCommand("Insert into student (id) values ("+ idTextBox.Text +")", cn); try { cn.Open(); cmd.ExecuteNonQuery; resultLabel.Text = "added successfully"; } catch (SqlException ex) { resultLabel.Text = ("error" + ex.Message); } finally { cn.Close(); } } 解决方案 这篇关于插入值到数据库表中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-18 18:20