查询数据库的问题 - 仅适用于我的PC

时间:2011-12-14 18:15:05

标签: c# ms-access iteration

我写了一些应该向List添加数据库响应的代码。它可以在我的电脑上运行,但是当我将程序部署到另一台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;
    }

我使用相对路径并测试它们。我也有我的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;
    }

4 个答案:

答案 0 :(得分:2)

确保可以从远程计算机访问该路径(例如,在远程计算机或某处的网络共享上),并且该用户可以访问该程序正在运行的路径。

答案 1 :(得分:1)

问题在于您的连接字符串更改或使用此替换

测试您的连接字符串
strAccessConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\yourpath\+yourAccessDataBase.mdb;Jet OLEDB:Database Password=if you have a password put here;"

答案 2 :(得分:1)

请尝试以下方法:
在第一个分组之前声明列表B.然后在for循环中的第一个while组中添加第一个B项。在退出第一组时添加rs.MoveNext();

答案 3 :(得分:1)

基本上这就是你可以做的......当然你需要在必要时用变量名替换某些东西

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)
         {

         }
    }           
}