不存在数据时尝试读取无效

发布于 2024-07-27 19:21:36 字数 687 浏览 7 评论 0原文

private void button1_Click(object sender, EventArgs e)
{
    string name;
    name = textBox5.Text;
    SqlConnection con10 = new SqlConnection("con strn");
    SqlCommand cmd10 = new SqlCommand("select * from sumant where username=@name");
    cmd10.Parameters.AddWithValue("@name",name);
    cmd10.Connection = con10;
    cmd10.Connection.Open();//line 7
    SqlDataReader dr = cmd10.ExecuteReader();
}

if ( textBox2.Text == dr[2].ToString())
{
    //do something;
}

当我调试到第 7 行时,一切正常,但之后 dr 抛出异常:

当不存在数据时,读取尝试无效。

我不明白为什么我会得到这个异常,因为我的表中确实有用户名 = sumant 的数据。

请告诉我“if”语句是否正确。 我该如何修复该错误?

private void button1_Click(object sender, EventArgs e)
{
    string name;
    name = textBox5.Text;
    SqlConnection con10 = new SqlConnection("con strn");
    SqlCommand cmd10 = new SqlCommand("select * from sumant where username=@name");
    cmd10.Parameters.AddWithValue("@name",name);
    cmd10.Connection = con10;
    cmd10.Connection.Open();//line 7
    SqlDataReader dr = cmd10.ExecuteReader();
}

if ( textBox2.Text == dr[2].ToString())
{
    //do something;
}

When I debug until line 7, it is OK, but after that dr throws an exception:

Invalid attempt to read when no data is present.

I don't understand why I'm getting that exception, since I do have data in the table with username=sumant.

Please tell me whether the 'if' statement is correct or not. And how do I fix the error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泛滥成性 2024-08-03 19:21:37

在尝试读取任何数据之前,您必须调用 dr.Read()。 如果没有任何内容可读取,该方法将返回 false。

You have to call dr.Read() before attempting to read any data. That method will return false if there is nothing to read.

海螺姑娘 2024-08-03 19:21:37

我刚刚遇到此错误,我调用的是 dr.NextResult() 而不是 dr.Read()

I just had this error, I was calling dr.NextResult() instead of dr.Read().

纵性 2024-08-03 19:21:37

我会检查 SqlDataReader 是否首先返回行:

SqlDataReader dr = cmd10.ExecuteReader();
if (dr.HasRows)
{
   ...
}

I would check to see if the SqlDataReader has rows returned first:

SqlDataReader dr = cmd10.ExecuteReader();
if (dr.HasRows)
{
   ...
}
平安喜乐 2024-08-03 19:21:37

我使用了下面的代码,它对我有用。

String email="";
    SqlDataReader reader=cmd.ExecuteReader();
    if(reader.Read()){
        email=reader["Email"].ToString();
    }

String To=email;

I used the code below and it worked for me.

String email="";
    SqlDataReader reader=cmd.ExecuteReader();
    if(reader.Read()){
        email=reader["Email"].ToString();
    }

String To=email;
悲凉≈ 2024-08-03 19:21:37

我有 2 个可能包含空值的值。

while(dr.Read())
 {
    Id = dr["Id"] as int? ?? default(int?);
    Alt =  dr["Alt"].ToString() as string ?? default(string);
    Name = dr["Name"].ToString()
 }

解决了问题

I was having 2 values which could contain null values.

while(dr.Read())
 {
    Id = dr["Id"] as int? ?? default(int?);
    Alt =  dr["Alt"].ToString() as string ?? default(string);
    Name = dr["Name"].ToString()
 }

resolved the issue

笑梦风尘 2024-08-03 19:21:36

您必须致电 DataReader.Read() 获取结果:

SqlDataReader dr = cmd10.ExecuteReader();
if (dr.Read()) 
{
    // read data for single/first record here
}

DataReader.Read() 返回一个 bool 指示是否还有更多数据块要读取,因此如果您有超过 1 个结果,您可以执行以下操作:

while (dr.Read()) 
{
    // read data for each record here
}

You have to call DataReader.Read() to fetch the result:

SqlDataReader dr = cmd10.ExecuteReader();
if (dr.Read()) 
{
    // read data for single/first record here
}

DataReader.Read() returns a bool indicating if there are more blocks of data to read, so if you have more than 1 result, you can do:

while (dr.Read()) 
{
    // read data for each record here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文