插入值到数据库表中的错误 [英] error in insert value into table of database

查看:187
本文介绍了插入值到数据库表中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在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();
}
}

解决方案

我发现错误



它应该是

cmd.ExecuteNonQuery();


你做了2个错误



1. SqlCommand值没有'

2.执行非查询应该像这样cmd.ExecuteNonQuery()



< pre lang =c#> 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();
        }
    }

解决方案

i found the error

it just should be
cmd.ExecuteNonQuery();


you have done 2 mistake

1. SqlCommand values does not have '
2. Eecute non query should like this cmd.ExecuteNonQuery()

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();
        }
    }


Use parameterized query, check this out[^]


这篇关于插入值到数据库表中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆