简体   繁体   English

用户代码未处理SqlException吗?

[英]SqlException was unhandled by user code?

I'm currently started in asp.net C# on school, and now they want me to insert data into SQL Server. 我目前在学校使用asp.net C#开始学习,现在他们希望我将数据插入SQL Server。 Now I came so far with this code, but when I try to add something into the database, I get the error: 现在我已经有了这段代码,但是当我尝试向数据库中添加内容时,出现了错误:

SqlException was unhandled by user code 用户代码未处理SqlException

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常,但未在用户代码中处理

Additional information: Column name or number of supplied values does not match table definition. 附加信息:列名或提供的值数与表定义不匹配。

Can someone explain me what I am doing wrong here? 有人可以解释一下我在做什么错吗?

Thanks in advance. 提前致谢。

Fred. 弗雷德。

public partial class AddMovie : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["VideoStoreConnectionString"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
            con.Open();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
            SqlCommand cmd = new SqlCommand("INSERT INTO dbo.Movie values('" + txtmovie.Text + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();
            Label1.Visible = true;
            Label1.Text = "Your Movie is Stored Successfully!";
            txtmovie.Text = "";
    }
}

If you have an insert command and do not want to specify the columns where you want to insert values then you need to supply VALUES for all the columns in the table in the correct order in which the columns appears inside the table. 如果您有插入命令,并且不想指定要在其中插入值的列,则需要以正确的顺序为表中的所有列提供VALUES,以使列在表中出现的顺序正确。

In this case I suppose that your table Movie has more columns than just the one supposed to contain the movie name (IE Title ?). 在这种情况下,我假设您的表Movie具有比应该包含电影名称(IE标题?)的列更多的列。 So you need to write the values for all columns or specify which column should receive the value for the txtmovie textbox 因此,您需要为所有列写值,或指定哪一列应接收txtmovie文本框的值

SqlCommand cmd = new SqlCommand(@"INSERT INTO dbo.Movie (Title) 
                                values(.....) ....

Said that, this is not the correct way to supply a command text to your database. 就是说,这不是向数据库提供命令文本的正确方法。
You should always use a parameterized query 您应该始终使用参数化查询

protected void Button1_Click(object sender, EventArgs e)
{
    string cmdText = @"INSERT INTO dbo.Movie  (Title) values(@name)";

    using(SqlConnection con = new SqlConnection(.......constring here...))
    using(SqlCommand cmd = new SqlCommand(cmdText, con))
    { 
        con.Open();
        cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = txtmovie.Text;
        cmd.ExecuteNonQuery();
        ....
    }
}

Finally, generally speaking, the best approach in using an SqlConnection (or every other kind of Disposable object) is to create it locally inside a using statement without keeping it in a global variable and opening/closing it in different places. 最后,通常来说,使用SqlConnection (或其他所有类型的Disposable对象)的最佳方法是在using语句内本地创建它,而不必将其保留在全局变量中并在不同位置打开/关闭它。
The Using Statement is very useful for this approach because it guarantees that the connection is closed and disposed also in case of exceptions. using语句对于此方法非常有用,因为它可以确保在发生异常的情况下也可以关闭并释放连接。 If you can try always to keep at minimum the number of global objects. 如果可以尝试始终将全局对象的数量保持在最少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM