我写了一些代码,应该将数据库响应添加到列表中。它可以在我的PC上运行,但是在将程序部署到另一台PC上时不起作用。
我固定我的方法。碰到就停止工作
int tot = rs.Fields.Count;
这是我的代码:
public static List<List<String>> QUERY(String query, String dbPath)
{ ADODB.Connection cn = new ADODB.Connection();
ADODB.Recordset rs = new ADODB.Recordset(); ADODB.Command cmdSQLData = new ADODB.Command();
List<List<String>> RETURNME = new List<List<String>>();
string cnStrOld = "Driver={Microsoft Access Driver (*.mdb)}; Dbq=" + dbPath + ";Uid=;Pwd=;"; //does not work
string cnStr = @"Provider=Microsoft.JET.OLEDB.4.0; data source=" + dbPath;
cn.ConnectionTimeout = 0;
cn.Open(cnStr);
cn.CommandTimeout = 0;
rs.Open(query, cn);
while (rs.EOF == false) //GET HEADERNAMES, ADD TO LIST
{
List<String> A = new List<string>();
int tot = rs.Fields.Count;// calculating the amount of columns in the RS
for (int i = 0; i < tot; i++) //iterating through all columns and checking it's name
{
A.Add(rs.Fields[i].Name.ToString());
}
RETURNME.Add(A);
break;
}
while (rs.EOF == false)//GET DATA, ADD TO LIST
{
List<String> B = new List<string>(); //list of Data
int tot = rs.Fields.Count;// calculating the amount of columns in the RS
//Now we add query response
for (int i = 0; i < tot; i++) //iterating through all columns and checking it's name
{
B.Add(rs.Fields[i].Value.ToString());
}
RETURNME.Add(B);
rs.MoveNext();
}
rs.Close();
cn.Close();
return RETURNME;
}
我使用相对路径,它们已经过测试。我也有尝试捕获的位置(我从这里删除了它们以缩小代码),它们表示没有错误。程序可以以某种方式进入“ while(rs.EOF == false)”语句,因此我假设返回了记录?
你能帮忙吗?
我得到了以下解决方案:
public static List<List<String>> QUERY_TEST(String query, String dbPath)
{
List<List<String>> RETURNME = new List<List<String>>();
String cnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath + ";Jet OLEDB:Database Password=;";
OleDbDataAdapter Data1 = new OleDbDataAdapter(query, cnStr);
DataSet a = new DataSet();
Data1.Fill(a);
DataTable dt = a.Tables[0];
//Adding column names to the first row on the list
List<String> B = new List<string>();
foreach (DataColumn dr in dt.Columns)
{
List<String> A = new List<string>();
B.Add(dr.Caption.ToString());
}
RETURNME.Add(B);
//Adding data to the columns
foreach (DataRow dr in dt.Rows)
{
List<String> A = new List<string>();
for (int i = 0; i < dt.Columns.Count; i++)
{
A.Add(dr[i].ToString());
}
RETURNME.Add(A);
break;
}
return RETURNME;
}
最佳答案
基本上,这就是您可以做的..当然,您需要在必要时用变量名替换某些内容
public static List<string> QUERY(string query, string dbpath)
{
string cnStr = @"Provider=Microsoft.JET.OLEDB.4.0; data source=" + dbPath;
oleconn = new OleDbConnection(cnStr);
List<string>lstColumns = null;
string[] strarryColumnNames = {}; //keep this just like this it's how you can declare dynamic array
string strCommaDelimColumns = string.Empty;
OleDbDataReader drdrRecord = null;
OleDbConnection oleconn = null;
using(OleDbCommand olecmd new OleDbCommand(query, cnStr))
{
olecmd.CommandTimeout = 60;
olecmd.CommandType = System.Data.CommandType.Text;
oleconn.Open();
//if you want to get the record count just setup a query with Select (count) as recCnt ....
//do the oledrd execute here.. then call oledrdr.Close(); and assign the other query string to another execute reader
/* basically do something like this and replace what I have with what you need
var drdrRecordCntReader = olecmd.ExecuteReader();
oledrdr.Read();
intRecCount = (int)oledrdr[recCnt];//value returned from the Select Count(*)
if (intRecCount == 0)
{
return false;
}//if (intRecCount == 0)
*/
oledrdr = olecmd.ExecuteReader();
lstColumns = new List<string>();
//load the field header contents here
for (int intCounter = 0; intCounter < oledrdr.FieldCount; intCounter++)
{
lstColumns.Add(oledrdr.GetName(intCounter));
}
strarrayColumnNames = lstColumns.ToArray();
strCommaDelimColumns = string.Join(",", strarryColumnNames);
//use the same lstColumns to add the data no need for a second while loop
//close the reader oledrdr
//use it again
try
{
//outter loop - Read row record by record
for (int intCounter = 0; intCounter < intRecCount - 1; intCounter++) // figure out how to get record count
{
rdrDataReader.Read();
//inner loop - read each field data within that row
for (int intFieldcnt = 0; intFieldcnt < intColumnCnt; intFieldcnt++)
{
//put your field data value code here
}//for (int intFieldcnt = 0; intFieldcnt < intColumnCnt; intFieldcnt++)
}
}
catch (Exception ex)
{
}
}
}
关于c# - 问题查询数据库-仅在我的PC上有效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8509575/