无效的操作异常 [英] Invalid operation exception

查看:120
本文介绍了无效的操作异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码。系统在行生成无效操作异常



com.ExecuteNonQuery();



请建议



SqlConnection con = new SqlConnection();

con.ConnectionString =Data Source = .\\SQLEXPRESS; Initial Catalog = master ;

string q =insert into category_name(category_name)values('+ TextBox1.Text +');

SqlCommand com = new SqlCommand(q,con );

com.ExecuteNonQuery();

Response.Write(alert('Category successfully registered'));

con。关闭();



我尝试过:



 SqlConnection con = new SqlConnection(); 
con.ConnectionString =Data Source = .\\SQLEXPRESS; Initial Catalog = master;
string q =insert into category_name(category_name)values('+ TextBox1.Text +');
SqlCommand com = new SqlCommand(q,con);
com.ExecuteNonQuery();
Response.Write(< script> alert('Category successfully registered')< / script>);
con.Close();

解决方案

不要这样做!永远不要连接字符串以形成SQL命令 - 总是使用参数化查询,否则您将面临SQL注入的风险,只需键入文本框即可让用户损坏或破坏您的数据库。



在您显示的代码中以及您应用中的其他任何地方修复它 - 您的问题可能会同时消失。


在您的代码中我看到您正在使用'掌握'数据库,这是一个系统数据库。我不认为你是故意使用这个。将数据库更改为您已在其中创建category_name表的数据库。同时使查询参数化而不是直接注入值,因为这容易受到sql注入攻击。对于参数化查询,你可以这样做..



string strQuery;



SqlCommand cmd;



strQuery =插入客户(CustomerID,CompanyName)值(@CustomerID,@ CompanyName);



cmd = new SqlCommand(strQuery);



cmd.Parameters.AddWithValue(@ CustomerID,A234);



cmd.Parameters.AddWithValue(@ CompanyName,DCB);


不是您问题的解决方案,而是您遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

i am using below code. system is generating invalid operation exception at line

com.ExecuteNonQuery();

please suggest

SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=master";
string q = "insert into category_name(category_name) values('" + TextBox1.Text + "')";
SqlCommand com = new SqlCommand(q, con);
com.ExecuteNonQuery();
Response.Write("alert('Category successfully registered')");
con.Close();

What I have tried:

SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=master";
        string q = "insert into category_name(category_name) values('" + TextBox1.Text + "')";
        SqlCommand com = new SqlCommand(q, con);
        com.ExecuteNonQuery();
        Response.Write("<script>alert('Category successfully registered')</script>");
        con.Close();

解决方案

Don't do it like that! Never concatenate strings to form an SQL command - always use Parameterized queries instead or you are at risk of SQL Injection which lets users damage or destroy your database just by typing in a textbox.

Fix that in the code you show - and everywhere else in your app - and your problem will probably go away at the same time.


In your code I see you are using 'master' database, this is is a system database. I don't think you are intentionally using this. Change the database to the one in which you have created category_name table. Also make the query parameterized instead of injecting values directly as this is vulnerable to sql injection attack. For parameterized query you can do like this..

string strQuery;

SqlCommand cmd;

strQuery = "insert into customers (CustomerID, CompanyName) values(@CustomerID, @CompanyName)";

cmd = new SqlCommand(strQuery);

cmd.Parameters.AddWithValue("@CustomerID", "A234");

cmd.Parameters.AddWithValue("@CompanyName", "DCB");


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]


这篇关于无效的操作异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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