C# 问题查询数据库-仅适用于我的电脑
我编写了一些代码,可以将数据库响应添加到列表中。它在我的电脑上工作,但在我将程序部署到另一台电脑上时不工作。 我坚持我的方法。当它击中时停止工作C# 问题查询数据库-仅适用于我的电脑,c#,ms-access,iteration,C#,Ms Access,Iteration,我编写了一些代码,可以将数据库响应添加到列表中。它在我的电脑上工作,但在我将程序部署到另一台电脑上时不工作。 我坚持我的方法。当它击中时停止工作 int tot = rs.Fields.Count; 这是我的密码: public static List<List<String>> QUERY(String query, String dbPath) { ADODB.Connection cn = new ADODB.Connection();
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;
}
公共静态列表查询(字符串查询,字符串dbPath)
{ADODB.Connection cn=new ADODB.Connection();
ADODB.Recordset rs=new ADODB.Recordset();ADODB.Command cmdSQLData=new ADODB.Command();
List RETURNME=新列表();
字符串cnStrOld=“Driver={Microsoft Access Driver(*.mdb)};Dbq=“+dbPath+”;Uid=;Pwd=;”;//不起作用
字符串cnStr=@“Provider=Microsoft.JET.OLEDB.4.0;数据源=“+dbPath;
cn.ConnectionTimeout=0;
cn.Open(cnStr);
cn.CommandTimeout=0;
rs.Open(查询,中国);
while(rs.EOF==false)//获取HEADERNAMES,添加到列表
{
列表A=新列表();
int tot=rs.Fields.Count;//计算rs中的列数
for(int i=0;i
我使用相对路径,它们测试正常。我还有我的try-catch语句(我从这里删除了它们以缩小代码),它们表示没有错误。不知何故,程序能够输入“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;
}
公共静态列表查询\u测试(字符串查询,字符串dbPath)
{
List RETURNME=新列表();
字符串cnStr=“Provider=Microsoft.Jet.OLEDB.4.0;数据源=“+dbPath+”;Jet-OLEDB:Database Password=;”;
OleDbDataAdapter Data1=新的OleDbDataAdapter(查询,cnStr);
数据集a=新数据集();
数据1.填写(a);
数据表dt=a.表[0];
//将列名添加到列表的第一行
列表B=新列表();
foreach(数据列dr在dt.列中)
{
列表A=新列表();
添加(dr.Caption.ToString());
}
加上(B);
//向列添加数据
foreach(数据行dr在dt.行中)
{
列表A=新列表();
对于(int i=0;i
确保路径可以从远程计算机访问(例如,在远程计算机或某个网络共享上),并且程序运行的用户可以访问该路径。确保路径可以从远程计算机访问(例如,在远程计算机或某个网络共享上)并可供程序运行时的用户访问。问题的关键在于连接字符串更改或使用此选项测试连接字符串
strAccessConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\yourpath\+yourAccessDataBase.mdb;Jet OLEDB:Database Password=if you have a password put here;"
问题的关键在于您的连接字符串更改或使用此选项测试您的连接字符串
strAccessConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\yourpath\+yourAccessDataBase.mdb;Jet OLEDB:Database Password=if you have a password put here;"
请尝试以下操作:在第一个while组之前声明列表B。然后在for循环的第一个while组中添加第一个B项。最后在退出第一个while组之前添加rs.MoveNext() 尝试以下操作:
在第一个while组之前声明列表B。然后在for循环的第一个while组中添加第一个B项。最后在退出第一个while组之前添加rs.MoveNext() 基本上这就是你能做的。。当然,您需要在必要时用变量名替换某些内容
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)
{
}
}
}
公共静态列表查询(字符串查询,字符串dbpath)
{
字符串cnStr=@“Provider=Microsoft.JET.OLEDB.4.0;数据源=“+dbPath;
OLECTONN=新的OLEDB连接(cnStr);
ListlstColumns=null;
string[]strarryColumnNames={};//保持如下状态这是声明动态数组的方式
string strCommaDelimColumns=string.Empty;
OleDbDataReader drdrRecord=null;
OLEDB连接OLECTONN=null;
使用(OLEDBCOMAND olecmd新OLEDBCOMAND(查询,cnStr))
{
olecmd.CommandTimeout=60;
olecmd.CommandType=System.Data.CommandType.Text;
尺骨鹰嘴开();
//如果要获取记录计数,只需使用Select(count)as recCnt…设置一个查询。。。。
//在此处执行oledrd。然后调用oledrdr.Close();并将另一个查询字符串分配给另一个执行读取器
/*基本上是这样做,用你需要的替换我拥有的
var drdrRecordCntReader=olecmd.ExecuteReader();
oledrdr.Read();
intRecCount=(int)oledrdr[recCnt];//从Select Count返回的值(*)
如果(intRecCount==0)
{
返回false;
}//如果(intRecCount==0)
*/
oledrdr=olecmd.ExecuteReader();
lstColumns=新列表();
//在此处加载字段标题内容
对于(int-intCounter=0;intCounter