C# 在OleDb的帮助下尝试从数据库读取数据时,我遇到了异常
您好,我正在尝试使用OleDb连接数据库,但当我想从那个里读取数据并在那个之后使用read()命令时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()) {
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语句