[英]How to get multiple rows from access database
I am trying to store each row of a access database, based on column Veh_ID
. 我试图基于列
Veh_ID
存储访问数据库的每一行。 The found data may or may not be based on multiple rows. 找到的数据可能基于也可能不是基于多行。 The code I am currently using can copy single row but if there are multiple results I can only get the first result.
我当前使用的代码可以复制单行,但是如果有多个结果,我只能得到第一个结果。 Can anyone please help me on this?
有人可以帮我吗? I am noob when it comes to database.
对于数据库,我是菜鸟。 I tried to search Google but no one seems to be needing what I need.
我尝试搜索Google,但似乎没有人需要我所需要的东西。 Here's the code I'm using:
这是我正在使用的代码:
string cmd1 = "SELECT * FROM Veh_checkup WHERE Veh_ID = " + veh_idd + "";
OleDbCommand cmd = new OleDbCommand(cmd1, con);
OleDbDataReader read = cmd.ExecuteReader();
read.Read();
veh_id=null;
int i=0;
foreach (var a in read)
{
try
{
veh_id = veh_id + " " + read[i].ToString();
}
catch { }
i++;
}
There are a few things I would point out, some specific to your question, some not: 我要指出几件事,有些是针对您的问题的,有些不是:
OleDbDataReader.Read()
to move to the next record. OleDbDataReader.Read()
移至下OleDbDataReader.Read()
记录。 StringBuilder
to concatenate strings in a loop, using string = string + "something"
will create a new string on the heap with each iteration StringBuilder
在循环中连接字符串,使用string = string + "something"
将在每次迭代时在堆上创建一个新字符串 using
blocks on Disposable objects using
块 catch { }
is not good practice . catch { }
不是好习惯 。 You will never know an error occurred. OleDbDataReader[i]
will get the data from column i
for the current record being read, not the data from row i
OleDbDataReader[i]
将从正在读取的当前记录的第i
列获取数据,而不是从第i
行获取数据 SELECT *
in production code, especially if you are only using 1 column. SELECT *
,尤其是在仅使用1列的情况下。 It is unnecessary data retrieval from the database and also unnecessary network traffic. Okay, I know I included using parameterised queries twice, but that is how strongly I feel about it! 好的,我知道我两次使用了参数化查询,但这就是我的强烈感觉!
With the above changes made, your full code will become something like: 完成上述更改后,您的完整代码将变为:
static string GetStringData(string vehID)
{
StringBuilder builder = new StringBuilder();
string cmd1 = "SELECT Column1 FROM Veh_checkup WHERE Veh_ID = @VehID";
using (OleDbConnection con = new OleDbConnection("YourConnectionString"))
using (OleDbCommand cmd = new OleDbCommand(cmd1, con))
{
con.Open();
cmd.Parameters.AddWithValue("@VehID", vehID);
using (OleDbDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
builder.Append(" " + reader.GetString(0));
}
}
}
return builder.ToString();
}
You are using the datareader in a wrong way. 您以错误的方式使用了数据读取器。 Instead of calling it once like you do, you have to call the datareader in a while loop like this:
您不必像这样一次调用它,而必须在while循环中调用数据读取器,如下所示:
while(theDataReader.Read())
{
// do your stuff in a loop now
}
So using this approach in your code would look something like this: 因此,在您的代码中使用这种方法将如下所示:
string cmd1 = "SELECT * FROM Veh_checkup WHERE Veh_ID = " + veh_idd + "";
OleDbCommand cmd = new OleDbCommand(cmd1, con);
OleDbDataReader read = cmd.ExecuteReader();
veh_id=null;
con.Open();
while(read.Read()) //your reader
{
try
{
veh_id = veh_id + " " + read[i].ToString();
}
catch { }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.