位置0没有行 [英] There is no row at position 0

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

问题描述

大家好!我面临类似的问题,我不知道如何解决他们我尝试了很多东西,但这个问题我还不能解决请帮助

实际问题是我想从数据库中选择值,如果ds == null所以文本框值我要插入数据库但是当我插入它时显示错误

Hello Guys! i am facing a similar problem and i dont know how to solve them i tried many things but this issue i can't solve yet please help
actually problem is i want to select value from database and if ds == null so textbox value i want to insert in database but when i insert it's shown me error

There is no row at position 0



这里是代码


here is the code

string query = "Select * From Contacts where [Phone No] = " + textBox00.Text + "";
                    using (OleDbConnection conn = new OleDbConnection(connStr))
                    {
                        using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
                        {
                            conn.Open();
                            DataSet ds = new DataSet();
                            if (ds != null)
                            {
                                adapter.Fill(ds);
                                label128.Text = ds.Tables[0].Rows[0]["Name"].ToString();
                                setaluefortext00001 = ds.Tables[0].Rows[0]["Phone No"].ToString();
                                setaluefortext00002 = ds.Tables[0].Rows[0]["Address"].ToString();
                                conn.Close();   
                            }
                            else
                            {
                                adapter.Fill(ds);
                                connection.Open();
                                OleDbCommand command1 = new OleDbCommand();
                                command1.Connection = connection;
                                command1.CommandText = "insert into Contacts ([Name],[Phone No],[Address])values('" + textBox.Text + "'," + textBox00.Text + ",'" + textBox000.Text + "');";
                                command1.ExecuteNonQuery();
                                connection.Close();
                                label128.Text = textBox.Text;
                                setaluefortext00002 = textBox00.Text;
                                setaluefortext00003 = textBox000.Text;
                            }





我的尝试:





What I have tried:

string query = "Select * From Contacts where [Phone No] = " + textBox00.Text + "";
                    using (OleDbConnection conn = new OleDbConnection(connStr))
                    {
                        using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
                        {
                            conn.Open();
                            DataSet ds = new DataSet();
                            if (ds != null)
                            {
                                adapter.Fill(ds);
                                label128.Text = ds.Tables[0].Rows[0]["Name"].ToString();
                                setaluefortext00001 = ds.Tables[0].Rows[0]["Phone No"].ToString();
                                setaluefortext00002 = ds.Tables[0].Rows[0]["Address"].ToString();
                                conn.Close();   
                            }
                            if (ds == null)
                            {
                                adapter.Fill(ds);
                                connection.Open();
                                OleDbCommand command1 = new OleDbCommand();
                                command1.Connection = connection;
                                command1.CommandText = "insert into Contacts ([Name],[Phone No],[Address])values('" + textBox.Text + "'," + textBox00.Text + ",'" + textBox000.Text + "');";
                                command1.ExecuteNonQuery();
                                connection.Close();
                                label128.Text = textBox.Text;
                                setaluefortext00002 = textBox00.Text;
                                setaluefortext00003 = textBox000.Text;
                            }

推荐答案

我从哪里开始?

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



其次,这个测试是多余的:

Where do I start?
Firstly, 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.

Secondly, this test is redundant:
if (ds != null)

因为你总是在它之前这样做:

Because you always do this before it:

DataSet ds = new DataSet();

ds 永远不能为空。



第三,不要使用魔术数字!如果您要使用绝对值来访问单行,那么您必须先测试以确保根本就有一行:

ds can never be null.

Thirdly, don't use magic numbers! If you are going to use absolute values to access a single row, then you have to test first to ensure that there is a row at all:

label128.Text = ds.Tables[0].Rows[0]["Name"].ToString();

你不检查,所以如果没有电话号码的行是完全匹配,您将获得您显示的例外。检查行数,如果它是零则不要继续。



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

You don't check, so if there are no rows where the phone number is an exact match, you will get the exception you show. Check the row count, and don't proceed if it is zero.

And 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... "label128"? That's going to be a very, very busy UI you got there...


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

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