Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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#SQL Server_C#_Sql Server - Fatal编程技术网

更新查询C#SQL Server

更新查询C#SQL Server,c#,sql-server,C#,Sql Server,单击EditAll按钮时会引发异常 private void editAll_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DELL\source\repos\phoneBookwin\phoneBookwin\Datab

单击EditAll按钮时会引发异常

private void editAll_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DELL\source\repos\phoneBookwin\phoneBookwin\Database1.mdf;Integrated Security=True");
            con.Open();

            string value;
            bool friendCheck = newFriends.Checked;
            bool familyCheck = newFamily.Checked;
            bool emergencyCheck = newEmergency.Checked;
            bool collCheck = newColl.Checked;
            if (friendCheck)
                value = newFriends.Text;
            else if (familyCheck)
                value = newFamily.Text;
            else if (emergencyCheck)
                value = newEmergency.Text;
            else if (collCheck)
                value = newColl.Text;
            else
                value = "";
            SqlCommand cmd = new SqlCommand("update Contacts set Name='"+newName.Text+"' Contacts='"+newNumber.Text+"' Email='"+newEmail.Text+"' Group='"+value+"' where Name='"+changeName.Text+"')", con);
            cmd.ExecuteNonQuery();
            this.Hide();
            Form1 save = new Form1();
            save.ShowDialog();
            con.Close();
        }
引发的异常是

联系人附近的语法不正确


但是,您的SQL看起来无效;这可能是一件好事,因为现在这是一个巨大的安全漏洞-您必须在SQL中使用参数,否则使用字符串连接方法将出现以下问题:

  • 安全性,请参阅“SQL注入”
  • 正确性,参见“Peter O'Toole”等名称
  • 正确性,在不同的客户端区域设置格式(特别是日期和数字)
  • 服务器性能(查询计划缓存)
参数代码很枯燥,但像“Dapper”这样的工具使其变得轻松:


//请参阅此处的“使用”-我们需要处理连接!
使用var con=newsqlconnection(…);
// ...
//(以下是Dapper添加的扩展方法)
con.Execute(@)
更新联系人
set Name=@newName,Contacts=@newNumber,
Email=@newEmail,Group=@newGroup
其中Name=@changeName“,new{
newName=newName.Text,
newNumber=newNumber.Text,
newEmail=newEmail.Text,
newGroup=值,
changeName=changeName.Text
});

还请注意,名称不是唯一的,不应(单独)用作谓词。

您的sql缺少一些逗号使其有效。这样连接sql容易受到sql注入攻击。您需要在设置的列之间使用逗号引发什么异常(消息是什么?异常类型是什么?)以及具体在哪一行?查看正确的sql语法,尝试直接在服务器上执行查询,并检查文档中的查询参数(永远不要相信用户输入!),您已经被告知要对前面问题中的sql语句进行参数化。请花点时间学习正确编写代码。