Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在OleDb的帮助下尝试从数据库读取数据时,我遇到了异常_C#_Oledbdatareader_Oledbexception - Fatal编程技术网

C# 在OleDb的帮助下尝试从数据库读取数据时,我遇到了异常

C# 在OleDb的帮助下尝试从数据库读取数据时,我遇到了异常,c#,oledbdatareader,oledbexception,C#,Oledbdatareader,Oledbexception,您好,我正在尝试使用OleDb连接数据库,但当我想从那个里读取数据并在那个之后使用read()命令时 cmd.Parameters.Add("@name", TextBox1.Text); cmd.Parameters.Add("@password", TextBox2.Text); cmd.ExecuteNonQuery(); System.Data.OleDb.OleDbDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) {

您好,我正在尝试使用OleDb连接数据库,但当我想从那个里读取数据并在那个之后使用read()命令时

cmd.Parameters.Add("@name", TextBox1.Text);
cmd.Parameters.Add("@password", TextBox2.Text);
cmd.ExecuteNonQuery();
System.Data.OleDb.OleDbDataReader rdr = cmd.ExecuteReader();
while (rdr.Read()) 
{                      
    string istifadeciAd = (string)rdr.GetString(1);
    string istifadeciParol = (string)rdr.GetString(2);
}

在字符串istifadeciAd和istifadeciParol中,由于IndexoutofRange,我得到了GetString的错误。但是我们不需要用列索引调用GetString吗

在select语句中需要有两个以上的select列才能获取
rdr.GetString(2)

像下面这样

从表1中选择id、istifadeciAd、istifadeciParol,其中name=?密码=?

请注意,索引是以零为基础的

因此,如果只选择了
istifadeciAd、istifadeciParol
列,则需要将其作为

string istifadeciAd = rdr.GetString(0);
string istifadeciParol = rdr.GetString(1);
您不需要将结果强制转换为
string
,因为它将返回
string

我想你也需要修改参数添加代码

cmd.Parameters.AddWithValue("@name", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);

在select语句中需要有两个以上的select列才能获取
rdr.GetString(2)

像下面这样

从表1中选择id、istifadeciAd、istifadeciParol,其中name=?密码=?

请注意,索引是以零为基础的

因此,如果只选择了
istifadeciAd、istifadeciParol
列,则需要将其作为

string istifadeciAd = rdr.GetString(0);
string istifadeciParol = rdr.GetString(1);
您不需要将结果强制转换为
string
,因为它将返回
string

我想你也需要修改参数添加代码

cmd.Parameters.AddWithValue("@name", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);

显示select语句显示select语句