Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类型为';System.Data.SqlClient.SqlException';发生在System.Data.dll中,但未在用户代码中处理。无法解决问题_C#_Sql_Asp.net_Visual Studio 2013 - Fatal编程技术网

C# 类型为';System.Data.SqlClient.SqlException';发生在System.Data.dll中,但未在用户代码中处理。无法解决问题

C# 类型为';System.Data.SqlClient.SqlException';发生在System.Data.dll中,但未在用户代码中处理。无法解决问题,c#,sql,asp.net,visual-studio-2013,C#,Sql,Asp.net,Visual Studio 2013,我尝试过使用其他人在这里使用的方法来解决他们的问题,但没有一个对我有效。我是ASP.NET框架的新手,无法理解为什么我试图发送到数据库的信息不起作用。另外,在我尝试提交新数据之前,Visual Studio不会给em一个错误,这使得我很难找出问题所在 namespace ylena_exercise { public partial class KnockoutBind : System.Web.UI.Page { SqlConnection con = new

我尝试过使用其他人在这里使用的方法来解决他们的问题,但没有一个对我有效。我是ASP.NET框架的新手,无法理解为什么我试图发送到数据库的信息不起作用。另外,在我尝试提交新数据之前,Visual Studio不会给em一个错误,这使得我很难找出问题所在

namespace ylena_exercise
{
    public partial class KnockoutBind : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=ylena_exercise;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            con.Open();
            GridView1.Visible = false;
        }

        protected void AddBtn_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("insert into Customers ('"+numid.Text+"','"+txtcustomer.Text+"','"+txtcontact.Text+"','"+txtaddress.Text+"','"+txtcity.Text+"','"+numpostcode.Text+"','"+txtcountry.Text+"')",con);
            cmd.ExecuteNonQuery();
            con.Close();
            GridView1.DataBind();
            Label1.Visible = true;
            Label1.Text = "Data Stored Successfully!";
            numid.Text = "";
            txtcustomer.Text = "";
            txtcontact.Text = "";
            txtaddress.Text = "";
            txtcity.Text = "";
            numpostcode.Text = "";
            txtcountry.Text = "";                
        }
    }
}


我的数据有问题吗?或者问题可能是ExecuteOnQuery?

您在
insert
语句中遗漏了
,您的代码应该是这样的:

SqlCommand cmd = new SqlCommand("insert into Customers values('"+numid.Text+"',....
var command = "insert into customers (id, name, contact, address, city, postcode, country) values (@id, @name, @contact, @address, @city, @postcode, @country)";
SqlCommand cmd = new SqlCommand(command);
cmd.Parameters.AddWithValue("@id", numid.Text);
cmd.Parameters.AddWithValue("@name", txtcustomer.Text);
cmd.Parameters.AddWithValue("@contact", txtcontact.Text);
cmd.Parameters.AddWithValue("@address", txtaddress.Text);
cmd.Parameters.AddWithValue("@city", txtcity.Text);
cmd.Parameters.AddWithValue("@postcode", numpostcode.Text);
cmd.Parameters.AddWithValue("@country", txtcountry.Text);
cmd.ExecuteNonQuery();
此外,您还应始终使用,以避免:


在sql中包括列名,并设置如下值:

SqlCommand cmd = new SqlCommand("insert into Customers values('"+numid.Text+"',....
var command = "insert into customers (id, name, contact, address, city, postcode, country) values (@id, @name, @contact, @address, @city, @postcode, @country)";
SqlCommand cmd = new SqlCommand(command);
cmd.Parameters.AddWithValue("@id", numid.Text);
cmd.Parameters.AddWithValue("@name", txtcustomer.Text);
cmd.Parameters.AddWithValue("@contact", txtcontact.Text);
cmd.Parameters.AddWithValue("@address", txtaddress.Text);
cmd.Parameters.AddWithValue("@city", txtcity.Text);
cmd.Parameters.AddWithValue("@postcode", numpostcode.Text);
cmd.Parameters.AddWithValue("@country", txtcountry.Text);
cmd.ExecuteNonQuery();

[礼节:Soner Gönül&user2946329]

嘿,所以我设法弄明白了ExecuteOnQuery为什么给我出问题。事实证明,我只是没有将表列名称与SQL命令中的变量匹配


为所有的麻烦家伙道歉!尽管如此,我还是感谢所有有用的建议和建议。

请尝试以下sql方法:
插入客户(column1\u name,column2\u name)值('column1\u value',column2\u value)
通常
SqlException
文本包含sql server提供的错误文本。读得更准确些,也许你会意识到原因。或者将其发布在您的问题中,以便有人帮助您。
string.Format
不能防止SQL注入漏洞。应该提供准备好的语句。我已经用这种方法尝试过了,但在cmd.ExecuteNonQuery()上仍然出现错误;带着信息“用户代码未处理InvalidOperationException。如果我显得无助,对不起。谢谢你的建议。“我会那样试的。”加贝德123……不客气。尽管直接指定类型并使用Value属性比AddWithValue更好。检查此项:@Soner不幸的是,即使在进行了调整之后,它仍然会中断,并向我显示消息“System.Data.dll中发生了类型为'System.InvalidOperationException'的异常,但未在用户代码中处理”并且尚未删除ExecuteOnQuery连接属性initialized@gabed123...Are你确定数据库中的列数是7吗?是的@user2946329,我相信有7列。