附加信息:位置0没有行。 [英] Additional information: there is no row at position 0.

查看:89
本文介绍了附加信息:位置0没有行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,我正在使用c#win表单,但是今天我打开项目时将这个项目留了一段时间,我将一个表单添加到我的应用程序中,当我运行

所以我得到此错误

Hello Guys, I was working on c# win form but I leave this project for some time now today when I open my project and I add a form to my application and when I run
so am getting this error

Additional information: There is no row at position 0.



但在我的数据库中我有60000这个表的数据我不知道知道问题请大家帮帮我

谢谢



我的尝试:




but in my DB I have 60000 data of this table I don't know what the issue please guys help me
Thanks

What I have tried:

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBox2.Text == "")
            {
                return;
            }
            else
            {
                con_string.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source =|DataDirectory|\Restaurant.accdb;Persist Security Info=False";
                con_string.Open();
                DataSet dsa = new DataSet();
                DataTable dt = new DataTable();
                dsa.Tables.Add(dt);
                OleDbDataAdapter da = new OleDbDataAdapter();
                da = new OleDbDataAdapter("SELECT [Place Name],[State],[Country],[Latitude],[Longtitude] FROM [Address] where [Postal Code] = " + textBox2.Text + "", con_string);
                da.Fill(dt);
                if (dt.Rows[0][0] =="")
                {
                    MessageBox.Show("User Cannot be Found", "Change Password", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox3.Text = "";
                }
                textBox3.Text = dt.Rows[0][0].ToString();
                textBox5.Text = dt.Rows[0][0].ToString();
                textBox4.Text = dt.Rows[0][0].ToString();
                con_string.Close();
            }
        }

推荐答案

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



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

For starters, 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. 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

--'

其他一切都是评论。

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



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



第二,如果没有与邮政编码匹配的行,则不会返回任何行。如果没有返回任何行,那么任何访问它们的尝试都会给你一个没有行......的例外。请检查Rows.Count属性,如果没有行,甚至不要尝试填充文本框。



现在你没注意到的东西。 ..

1)文本框3,4和5都将获得相同的数据。

2)如果用户键入错误,请不要清除他的输入!选择它,并将焦点设置为该控件是 - 然后他知道什么是错误并且可以修复它。但是删除他输入的内容让他看不出他做错了什么只是粗鲁。

3)不要对连接字符串进行硬编码 - 当你转向制作时真的很痛苦。总是使用配置文件或类似文件。



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

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 second, if there are no rows that match the postcode, no rows will be returned. If no rows are returned, then any attempt to access them will give you a "There is no row at..." exception. Check the Rows.Count property instead, and don't even try to fill your textboxes if there are no rows.

Now for the stuff you haven't noticed...
1) Text boxes 3, 4, and 5 will all get the same data.
2) If the user types wrong, don't clear his input! Select it, and set the focus to that control yes - he then knows what is wrong and can fix it. But deleting what he typed so he can't see what he did wrong is just rude.
3) Don't hardcode connection strings - it's a real pain when you move to production. Always use a config file or similar.

BTW: 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...


这篇关于附加信息:位置0没有行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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