获取SQL错误我该怎么做 [英] Getting SQL Error What should I Do

查看:94
本文介绍了获取SQL错误我该怎么做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码我仍然收到错误



异常抛出:System.Data.dll中的'System.Data.SqlClient.SqlException'



附加信息:')'附近的语法不正确。



With this codes I m still getting error

Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll

Additional information: Incorrect syntax near ')'.

<pre lang="text"> private void insert_Click(object sender, RoutedEventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand (@"Insert into mob (FirstName, LastName, Mobile, emailid, Category) VALUES 
                ('"+ First.Text + "','" + Last.Text +"','" + Mobile.Text +"', '" + email.Text + "','" +comboBox + "',)", con);
            cmd.ExecuteNonQuery();
            con.Close();


        } 

推荐答案

在结尾处有一个额外的逗号(,) INSERT语句中的值列表。

试试这个 -

You have an additional comma (,) at the end of the list of values in the INSERT statement.
Try this-
SqlCommand cmd = new SqlCommand (@"Insert into mob (FirstName, LastName, Mobile, emailid, Category) VALUES
              ('"+ First.Text + "','" + Last.Text +"','" + Mobile.Text +"', '" + email.Text + "','" +comboBox + "')", con);



注意:您的代码容易受到SQL注入攻击。我强烈建议使用参数化查询或存储过程。

参考:

使用参数化查询来防止SQL Server中的SQL注入攻击 [ ^ ]



希望,它有帮助:)


Note: Your code is vulnerable to SQL Injection. I strongly recommend to use Parameterized Query or Stored Procedure instead.
Reference:
Using Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]

Hope, it helps :)


这篇关于获取SQL错误我该怎么做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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