使用 DataReader 从数据库读取 SQL 问题
我有一个用户表(tblUsers),其中包含大学工作人员的详细信息。我正在尝试使用与所选模块关联的讲师姓名填充文本框。
我正在获取与特定模块关联的所有用户 ID,测试用户是否是讲师,如果是,则将 ID 添加到 ArrayList。
然后,我迭代该数组,并在每次迭代过程中通过当前 ID 调用下面的方法。
但是,如果您查看下面的方法,我正在使用 SqlDataReader,并且在这一行读取时出现错误:
txtLecturerName.Text += myReader["First_Name"].ToString();
错误信息是: 'myReader["First_Name"]' 引发了 'System.IndexOutOfRangeException' 类型的异常
我正在使用的表布局位于方法代码下方。任何有关此问题的帮助将不胜感激,我只需一杯咖啡就可以将头伸进屏幕。
public void outputLecturerNames(string lecturerID)
{
// Create a new Connection object using the connection string
SqlConnection myConnection = new SqlConnection(conStr);
// If the connection is already open - close it
if (myConnection.State == ConnectionState.Open)
{
myConnection.Close();
}
// 'using' block allows the database connection to be closed
// first and then the exception handling code is triggered.
// This is a better approach than using a 'finally' block which
// would close the connection after the exception has been handled.
using (myConnection)
{
try
{
// Open connection to DB
myConnection.Open();
SqlCommand selectCommand = new SqlCommand(selectQuery, myConnection);
// Declare a new DataReader
SqlDataReader myReader;
selectQuery = "SELECT * FROM tblUsers WHERE User_ID='";
selectQuery += lecturerID + "'";
myReader = selectCommand.ExecuteReader();
while (myReader.Read())
{
txtLecturerName.Text += myReader["First_Name"].ToString();
txtLecturerName.Text += " ";
txtLecturerName.Text += myReader["Last_Name"].ToString();
txtLecturerName.Text += " , ";
}
myReader.Close();
}
catch (Exception err)
{
Console.WriteLine("Error: " + err);
}
}
}
表用户:
[User_ID][First_Name][Last_Name][Email_Address]
I have a table of Users (tblUsers) which contains details of University staff. I am trying to populate a text box with the names of lecturers associated with a selected module.
I am getting all UserIDs associated with a particular module, testing if the User is a lecturer, if so then I add the ID to an ArrayList.
I then iterate through this array and call the method below during each iteration passing through the current ID.
However, if you look at the method below I am using a SqlDataReader and am getting an error while reading from it on this line:
txtLecturerName.Text += myReader["First_Name"].ToString();
The error message is:
'myReader["First_Name"]' threw an exception of type 'System.IndexOutOfRangeException'
The table layout I am using is below the method code. Any help with this would be greatly appreciated, I am one cup of coffee away from putting my head through the screen.
public void outputLecturerNames(string lecturerID)
{
// Create a new Connection object using the connection string
SqlConnection myConnection = new SqlConnection(conStr);
// If the connection is already open - close it
if (myConnection.State == ConnectionState.Open)
{
myConnection.Close();
}
// 'using' block allows the database connection to be closed
// first and then the exception handling code is triggered.
// This is a better approach than using a 'finally' block which
// would close the connection after the exception has been handled.
using (myConnection)
{
try
{
// Open connection to DB
myConnection.Open();
SqlCommand selectCommand = new SqlCommand(selectQuery, myConnection);
// Declare a new DataReader
SqlDataReader myReader;
selectQuery = "SELECT * FROM tblUsers WHERE User_ID='";
selectQuery += lecturerID + "'";
myReader = selectCommand.ExecuteReader();
while (myReader.Read())
{
txtLecturerName.Text += myReader["First_Name"].ToString();
txtLecturerName.Text += " ";
txtLecturerName.Text += myReader["Last_Name"].ToString();
txtLecturerName.Text += " , ";
}
myReader.Close();
}
catch (Exception err)
{
Console.WriteLine("Error: " + err);
}
}
}
tblUsers:
[User_ID][First_Name][Last_Name][Email_Address]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的方法中,未声明变量 selectQuery,并且在为 tblUsers 分配查询字符串之前将其用作 SqlCommand 的参数。
In your method, the variable selectQuery is not declared, and it is used as parameter to SqlCommand before it is assigned the query string on tblUsers.
您可能拼错了列名称。
一般来说,您永远不应该编写
SELECT * FROM ...
。相反,您应该仅选择您需要的列。
这将使您的程序通过仅查询您需要的信息来运行得更快,并且可以产生更好的错误消息。
You've probably misspelled a column name.
In general, you should never write
SELECT * FROM ...
.Instead, you should select only the columns you need.
This will make your program run faster by only querying the information that you need, and can produce better error messages.
当未找到给定的列名时,会产生此错误。如果你和我一样,你可能已经检查过好几次了,但是表名是否正确(正确的数据库、正确的架构)以及列名是否正确?
http://msdn.microsoft.com/en-us/library/f01t4cfy。 aspx
您可以尝试完全限定表的名称 (database.dbo.tblUsers)。这将确保您能如愿以偿。另外,尝试将列名放入 SQL 语句中。如果它们不正确,您的 SQL 语句将无法正确执行。
This error is created when the column name given is not found. If you are anything like me, you've probably checked it several times, but is the table name correct (correct database, correct schema) and is the column name correct?
http://msdn.microsoft.com/en-us/library/f01t4cfy.aspx
You might try fully qualifying the name of the table (database.dbo.tblUsers). This would ensure that you are hitting the table you think you are. Also, try and put the names of the columns into the SQL statement. If they are not correct, your SQL statement will not execute properly.