如何在C#中验证数据读取器 [英] How to validate data reader in C#
问题描述
从学生中选择 *
Rollno Studname年龄DOB地址有效状态
1 Ajay 6 12.10.2010 Chennai A Male
2 Suresh 5 5.7.2011 Chennai A Male
3 Priya 6 10.6.2010 Chennai A FeMale
4 Revathy 5 8.9.2011 Chennai A FeMale
来自上表我的验证如下
使用(SqlConnection newCon = new SqlConnection(db.GetConnectionString))
{
SqlCommand newCmd = new SqlCommand(从学生中选择* ) ,newCon);
newCmd.Parameters.Add()
newCon.Open();
SqlDataReader rdr = newCmd.ExecuteReader();
rdr.Read();
if(reader [6] .ToString()== Male );
{
//我正在显示男性形象
}
if(reader [6] .Tostring()== FeMale );
{
//我正在显示FeMale图片
}
}
当我执行上述代码时显示错误如下
无效尝试没有数据时读取。
以上错误显示如下以下
if (reader [ 6 ]。ToString()== 男性)
我上面代码中的错误是什么。请帮帮我
i试用很多但显示错误。
你从DataReader读取,但是你不要检查是否返回任何行:所以当它们不是时,你会得到一个明确告诉你的错误!
首先检查返回值:
if (rdr.Read();
{
if (读者[ 6 ]。ToString()== 男性);
{
...并且即时错误将消失。
但是...你显示的代码不是你正在运行的代码,因为这不会编译:
SqlCommand newCmd = new SqlCommand(从学生中选择* ) < span class =code-string>,newCon);
我怀疑你的真实代码正在使用WHERE子句来选择返回信息的学生。并且WHERE子句不起作用...所以没有返回任何行。
我建议创建 Dictionary [ ^ ]对象具有以下功能:
字典<字符串,字符串> images = new 字典< string,string>();
images.Add( 男性, C:\ Pictures\Male.jpg);
images.Add( FeMale, C:\ Pictures\FeMale.jpg);
然后,您将能够根据学生的性别简单地指定图像路径。
PictureBox1.Image = Image.FromFile(images [reader [ 6 ]]);
详情请见:
在运行时设置图片(Windows窗体) [ ^ ]
Image.FromFile Method(字符串)(System.Drawing) [ ^ ]
Select * from student
Rollno Studname Age DOB Address Active Status 1 Ajay 6 12.10.2010 Chennai A Male 2 Suresh 5 5.7.2011 Chennai A Male 3 Priya 6 10.6.2010 Chennai A FeMale 4 Revathy 5 8.9.2011 Chennai A FeMale
from the above table i am validating as follows
using (SqlConnection newCon = new SqlConnection(db.GetConnectionString)) { SqlCommand newCmd = new SqlCommand(Select * from student)", newCon); newCmd.Parameters.Add() newCon.Open(); SqlDataReader rdr = newCmd.ExecuteReader(); rdr.Read(); if(reader[6].ToString() == "Male"); { //In that i am displaying Male Image } if(reader[6].Tostring() == "FeMale"); { //In that i am displaying FeMale Image } }
When i exeute the above code shows error as follows
Invalid attempt to read when no data is present.
The above error shows in below line as follows
if(reader[6].ToString() == "Male")
What is the mistake in my above code. please help me
i tried lot but shows error.解决方案You read from the DataReader, but you don't check that any rows are returned: so when they aren't you get an error which explicitly tells you just that!
Start by checking the return value:
if (rdr.Read(); { if(reader[6].ToString() == "Male"); { ...And the immediate error will go away.
But...the code you show isn't the code you are running, because this won't compile:
SqlCommand newCmd = new SqlCommand(Select * from student)", newCon);
And I suspect that your "real" code is using a WHERE clause to select which student to return info for. And that WHERE clause doesn't work...so no rows are returned.
I'd suggest to create Dictionary[^] object for such of functionality:
Dictionary <string, string> images = new Dictionary<string, string>(); images.Add("Male", "C:\Pictures\Male.jpg"); images.Add("FeMale", "C:\Pictures\FeMale.jpg");
Then you'll be able to simply assign path to image depending on student's gender.
PictureBox1.Image = Image.FromFile(images[reader[6]]);
For further details, please see:
Setting Pictures at Run Time (Windows Forms)[^]
Image.FromFile Method (String) (System.Drawing)[^]
这篇关于如何在C#中验证数据读取器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!