C#无法获取要填充的数据表 [英] C# cannot get the datatable to fill

查看:93
本文介绍了C#无法获取要填充的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于输入数据的文本框和一个用于搜索返回到datagridview的数据的按钮。数据没有传递给数据表。





I have a textbox to enter data and a button to search for that data returned to a datagridview. The data is not getting passed to the datatable.


private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source = AIRS1C49; Initial Catalog = BoundBook; Integrated Security = True");
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter sda = new SqlDataAdapter(@"SELECT Firearm.Serial_Number, Firearm.Manufacturer, Firearm.Country_of_Mfg, Firearm.Model, Firearm.Type, Firearm.[Action], Firearm.Gage_Caliber, Firearm.Status, Firearm.Acquired, Firearm.Disposed, Acquisition.Acq_ID,
                         Acquisition.Acq_Date, Acquisition.Acq_Reason, Acquisition.Serial_Number AS Expr1, Acquisition.Acq_Note, Acquisition.DataEntryDate_A, Acquisition.FFL_ID, FFL.FFL_ID AS Expr2, FFL.FFL_Name, FFL.FFL_Address1,
                         FFL.FFL_Address2, FFL.FFL_City, FFL.FFL_State, FFL.FFL_ZipCode, FFL.FFL_Email, FFL.FFL_Phone, FFL.FFL_License_Number, FFL.FFL_Expiration_Date, Disposition.Disp_ID, Disposition.Disp_Date, Disposition.Disp_Reason,
                         Disposition.Serial_Number AS Expr3, Disposition.FFL_ID AS Expr4, Disposition.Disp_Note, Disposition.DataEntryDate_D
                         FROM            Firearm INNER JOIN
                         Acquisition ON Firearm.Serial_Number = Acquisition.Serial_Number INNER JOIN
                         FFL ON Acquisition.FFL_ID = FFL.FFL_ID INNER JOIN
                         Disposition ON Firearm.Serial_Number = Disposition.Serial_Number AND FFL.FFL_ID = Disposition.FFL_ID
                         WHERE FireArm.Serial_Number = '" + textBox1.Text + "%'", con);
           
            DataSet dt = new DataSet();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;





我尝试过:



我尝试过使用较小的查询,但是得到的结果相同,没有任何内容传递给数据表。有人告诉我,我需要打开和关闭连接,所以我添加了一个con.Open();和con.Close();但这没有任何区别所以我删除了它们。我在另一个表单上使用相同的连接字符串,它在那里工作,所以我无法想象它是问题。



任何想法?



What I have tried:

I have tried using a smaller query, but I get the same results, nothing passed to the datatable. Someone told me that I needed to open and close the connection so I added a con.Open(); and con.Close(); but that made no difference so I removed them. I'm using the same connection string on another form and it works there so I can't imagine it being the problem.

Any ideas?

推荐答案

SqlDataAdapter sda = new SqlDataAdapter(@"SELECT Firearm.Serial_Number, Firearm.Manufacturer, Firearm.Country_of_Mfg, Firearm.Model, Firearm.Type, Firearm.[Action], Firearm.Gage_Caliber, Firearm.Status, Firearm.Acquired, Firearm.Disposed, Acquisition.Acq_ID,
             ...
             WHERE FireArm.Serial_Number = '" + textBox1.Text + "%'", con);



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

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

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

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


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[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


Ppolymorphe关于SQL注入风险:永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



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

Ppolymorphe is bang on about the SQL Injection risk: 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. Use Parametrized 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

--'

其他一切都是评论。

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



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



所以你需要做的第一件事就是完成你的整个应用程序并修复它 - 留下一个连接,你经常有更好的备份!



完成后,查看你的查询,当你没有得到你想要的数据时,从WHERE子句开始,因为这决定了返回什么数据:

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?

So the first thing you need to do is go through your entire app and fix that - leave one concatenation, and you had better backup often!

When you have done that, look at your query, and when you don't get the data you want, start with the WHERE clause, since that is what decided what data is returned:

... WHERE FireArm.Serial_Number = '" + textBox1.Text + "%'"

等于完全匹配;它不承认通配符。你的意思是:

Equals is an exact match; it doesn't acknowledge wildcards. Did you mean:

... WHERE FireArm.Serial_Number LIKE @SerNo + '%'"


总是在任何sql连接代码中

打开和关闭实例SqlConnection类。这非常重要
always in any sql connection code
open and close instance SqlConnection Class . this is very important


这篇关于C#无法获取要填充的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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