我试图连接到SQL服务器数据库,但它会引发错误。这是我尝试过的代码: [英] I am trying to connect to the SQL server database, but it throws an error. Here is the code I have tried :

查看:106
本文介绍了我试图连接到SQL服务器数据库,但它会引发错误。这是我尝试过的代码:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Data.dll中发生了'System.Data.SqlClient.SqlException'类型的异常,但未在用户代码中处理

i被困在这里,con.Open();



我的尝试:



An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
i am stuck here, con.Open();

What I have tried:




SqlConnection con = new SqlConnection(Data Source = DESKTOP-72CIIS5; Initial Catalog = user_desk; User ID = sa; Password = ***** ******);

SqlCommand cmd = new SqlCommand(insert into usertable values('+ textBox1.Text +','+ textBox2.Text +',' + textBox3.Text +','+ textBox4.Text +',),con);

con.Open();


SqlConnection con = new SqlConnection("Data Source=DESKTOP-72CIIS5;Initial Catalog=user_desk;User ID=sa;Password=***********");
SqlCommand cmd = new SqlCommand("insert into usertable values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "',)", con);
con.Open();

推荐答案

不要那样做!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你经常定期备份,不是吗?



如果你解决了这个问题,你的SQL的语法问题会非常非常明显:

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And if you fix that, the syntax problem with your SQL will be very, very obvious:

using (SqlCommand cmd = new SqlCommand("INSERT INTO usertable VALUES (@P1, @P2, @P3, @P4, @p5,)", con)
   {
   ...



另外:

1)总是列出你想插入的列成。这样,对你的表(和IDENTITY列)的更改就不会弄乱你的数据。

2)帮自己一个忙,并停止使用Visual Studio默认名称 - 你可能还记得 TextBox8是今天的手机号码,但是当你必须在三周内修改它时,你会这样吗?使用描述性名称 - 例如tbMobileNo - 您的代码变得更容易阅读,更自我记录,更易于维护 - 并且编码速度更快,因为Intellisense可以通过三次击键来tbMobile,其中TextBox8需要思考大概和8次击键...


In addition:
1) Always list the columns you wish to insert into. That way, changes to your table (and IDENTITY columns) don;t mess up your data.
2) Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...


看来你在错误的地方打开连接。



步骤跟随



1.创建Sql连接

2。打开连接

3.分配与命令的连接。



而在你的情况下你正在交换2和3.



更多使用参数而不是构建查询



这里有你的例子



It seems you are opening connection in wrong place.

steps to follow

1. Create Sql Connection
2. Open connection
3. assign connection to command.

while in your case your are swaping 2 and 3.

More over use parameters instead of building query

here is example for you

using (connection = new SqlConnection(connectionString))
{
  connection.Open();
  var command = new SqlCommand();
  command.CommandText = @"
     INSERT INTO [dbo].[Product]
           ([ProductId]
           ,[Name]
           ,[Description])           
     VALUES
          (@Pid,
           @Name,
           @Desc)";
 command.Connection = connection;
 command.Parameters.Add(new SqlParameter("@Pid", SqlDbType.VarChar, 100)).Value = product.ProductId;
 command.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 100)).Value = product.Name;
 command.Parameters.Add(new SqlParameter("@Desc", SqlDbType.VarChar, 255)).Value = product.Description;                
 command.ExecuteNonQuery();
}


这篇关于我试图连接到SQL服务器数据库,但它会引发错误。这是我尝试过的代码:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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