c#报错:在没有任何数据时进行无效的读取尝试。
下面是连接数据库的代码:usingSystem.Data;usingSystem.Data.SqlClient;usingSystem;usingSystem.Colle...
下面是连接数据库的代码:
using System.Data;
using System.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReportOrders
{
class Report
{
static void Main(string[] args)
{
SqlConnection dataConnection = new SqlConnection();
try
{
string username = "sa";
string password = "***";
string connString = String.Format("User ID={0};Password={1};Initial Catalog=ls;" + "Data Source=127.0.0.1", username, password);
dataConnection.ConnectionString = connString;
dataConnection.Open();
Console.Write("输入ID查询ls数据库的实验数据表(5 characters):");
string shiyanId = Console.ReadLine();
Console.WriteLine(shiyanId);
SqlCommand dataCommand = new SqlCommand();
dataCommand.Connection = dataConnection;
dataCommand.CommandType = CommandType.Text;
dataCommand.CommandText = "select ID,列1,列2 from 实验 where ID=@shiyanIdParam";
SqlParameter param = new SqlParameter("@shiyanIdParam", SqlDbType.Int,0);
param.Value = shiyanId;
dataCommand.Parameters.Add(param);
Console.WriteLine("shiyanId的值 {0}\n\n", shiyanId);
SqlDataReader dataReader = dataCommand.ExecuteReader();
string orderId = dataReader.GetString(0);
string lie1 = dataReader.GetString(1);
string lie2 = dataReader.GetString(2);
Console.WriteLine("orderId: {0}列1:{1}列2:{2}", orderId, lie1, lie2);
dataReader.Close();
}
catch (SqlException e)
{
Console.WriteLine("Error accessing the database: {0}", e.Message);
}
finally
{
dataConnection.Close();
}
}
}
}
调试报错:未经处理的异常: System.InvalidOperationException: 在没有任何数据时进行无效的读取尝试。
报错行是:string orderId = dataReader.GetString(0);
请问是怎么回事?
谢谢!! 展开
using System.Data;
using System.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReportOrders
{
class Report
{
static void Main(string[] args)
{
SqlConnection dataConnection = new SqlConnection();
try
{
string username = "sa";
string password = "***";
string connString = String.Format("User ID={0};Password={1};Initial Catalog=ls;" + "Data Source=127.0.0.1", username, password);
dataConnection.ConnectionString = connString;
dataConnection.Open();
Console.Write("输入ID查询ls数据库的实验数据表(5 characters):");
string shiyanId = Console.ReadLine();
Console.WriteLine(shiyanId);
SqlCommand dataCommand = new SqlCommand();
dataCommand.Connection = dataConnection;
dataCommand.CommandType = CommandType.Text;
dataCommand.CommandText = "select ID,列1,列2 from 实验 where ID=@shiyanIdParam";
SqlParameter param = new SqlParameter("@shiyanIdParam", SqlDbType.Int,0);
param.Value = shiyanId;
dataCommand.Parameters.Add(param);
Console.WriteLine("shiyanId的值 {0}\n\n", shiyanId);
SqlDataReader dataReader = dataCommand.ExecuteReader();
string orderId = dataReader.GetString(0);
string lie1 = dataReader.GetString(1);
string lie2 = dataReader.GetString(2);
Console.WriteLine("orderId: {0}列1:{1}列2:{2}", orderId, lie1, lie2);
dataReader.Close();
}
catch (SqlException e)
{
Console.WriteLine("Error accessing the database: {0}", e.Message);
}
finally
{
dataConnection.Close();
}
}
}
}
调试报错:未经处理的异常: System.InvalidOperationException: 在没有任何数据时进行无效的读取尝试。
报错行是:string orderId = dataReader.GetString(0);
请问是怎么回事?
谢谢!! 展开
3个回答
展开全部
先要调用dataReader.Read();方法才能对dataReader进行读取操作。
dataReader.Read();
string orderId = dataReader.GetString(0);
dataReader.Read();
string orderId = dataReader.GetString(0);
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlDataReader dataReader = dataCommand.ExecuteReader();
while(dataReader.read())
{
string orderId = dataReader.GetString(0);
string lie1 = dataReader.GetString(1);
string lie2 = dataReader.GetString(2);
}
Console.WriteLine("orderId: {0}列1:{1}列2:{2}", orderId, lie1, lie2);
dataReader.Close();
while(dataReader.read())
{
string orderId = dataReader.GetString(0);
string lie1 = dataReader.GetString(1);
string lie2 = dataReader.GetString(2);
}
Console.WriteLine("orderId: {0}列1:{1}列2:{2}", orderId, lie1, lie2);
dataReader.Close();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
主方法里写那么多代码,没注释
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询