C# SQLCommand和SqlDataReader错误
当不存在数据时尝试读取无效 此错误发生在此行上C# SQLCommand和SqlDataReader错误,c#,sql-server,wpf,sqldatareader,C#,Sql Server,Wpf,Sqldatareader,当不存在数据时尝试读取无效 此错误发生在此行上 string ingredientName = reader2.GetString(0); 我的代码: var ingredientList = new List<Ingredient>(); SqlCommand staffCommand = new SqlCommand(); string conString = EventsUnlimited.Properties.Settings.Default.DatabaseEvent
string ingredientName = reader2.GetString(0);
我的代码:
var ingredientList = new List<Ingredient>();
SqlCommand staffCommand = new SqlCommand();
string conString = EventsUnlimited.Properties.Settings.Default.DatabaseEventsUnlimitedConnectionString;
using (SqlConnection connection = new SqlConnection(conString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("SELECT IngredientID, Quantity FROM CourseIngredients WHERE CourseID =" + courseID, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// Obtains the columns from customer
int ingredientID = reader.GetInt32(0);
decimal quantity = reader.GetDecimal(1);
string ingredientNameConstruct;
SqlCommand ingredientCommand = new SqlCommand();
string conString2 = EventsUnlimited.Properties.Settings.Default.DatabaseEventsUnlimitedConnectionString;
using (SqlConnection connection2 = new SqlConnection(conString2))
{
connection2.Open();
using (SqlCommand command2 = new SqlCommand ("SELECT IngredientName FROM Stock WHERE IngredientID =" + ingredientID, connection2))
{
using (SqlDataReader reader2 = command2.ExecuteReader())
{
string ingredientName = reader2.GetString(0);
ingredientNameConstruct = ingredientName;
}
}
}
ingredientList.Add(new Ingredient(courseID, ingredientNameConstruct, ingredientID, quantity));
}
}
}
return ingredientList;
}
var ingredientList=new List();
SqlCommand staffCommand=新SqlCommand();
string conString=EventsUnlimited.Properties.Settings.Default.DatabaseEventsUnlimitedConnectionString;
使用(SqlConnection=newsqlconnection(conString))
{
connection.Open();
使用(SqlCommand命令=新SqlCommand(“从CourseIngElements中选择IngCreditId,数量,其中CourseID=“+CourseID,连接))
{
使用(SqlDataReader=command.ExecuteReader())
{
while(reader.Read())
{
//从客户处获取列
int-ingredientID=reader.GetInt32(0);
十进制数量=读卡器.GetDecimal(1);
字符串IngreditNameConstruction;
SqlCommand ingredientCommand=新SqlCommand();
string conString2=EventsUnlimited.Properties.Settings.Default.DatabaseEventsUnlimitedConnectionString;
使用(SqlConnection connection2=新的SqlConnection(conString2))
{
连接2.Open();
使用(SqlCommand command2=新SqlCommand(“从库存中选择IngredientName,其中IngredientID=“+IngredientID,connection2))
{
使用(SqlDataReader reader2=command2.ExecuteReader())
{
string IngreditName=reader2.GetString(0);
ingredientName构造=ingredientName;
}
}
}
添加(新成分(courseID、ingredientNameConstruct、ingredientID、quantity));
}
}
}
返回用户列表;
}
我不确定是什么导致了这个问题。表和我试图读取的行中都有数据。调用
reader2
DataReader中的方法以获得结果。您没有调用read()
方法来读取reader2
对象,该对象实际根据返回的结果读取一行,请在实际读取列值之前添加该方法:
using (SqlDataReader reader2 = command2.ExecuteReader())
{
if(reader2.Read()) // this is needed
{
string ingredientName = reader2.GetString(0);
ingredientNameConstruct = ingredientName;
}
}
如果需要多行,则使用while
循环,如果总是一行,则可以使用reader2.ExecuteScalar
作为注释中提到的@bradbury9:
string ingredientName = command2.ExecuteScalar()?.ToString();
ingredientNameConstruct = ingredientName;
如果您连接两个表,则可以通过单个查询完成此操作。您的第二个
读取器
没有读取,您还需要参数化查询以防止SQL注入。如上所述,您应该使用参数。如果您在sql中应用了正确的联接,并使用sql命令检索所有信息,那么代码也会更少,更易于阅读。