1

我有以下使用查询的方法,但 Visual Studio 会引发错误,如图所示。

private void FindOrderData()
        {
            int intOrder = int.Parse(frmInput.InvoiceOrder);

            string InvSql = "SELECT Orders.OrderID, Orders.OrderDate, Orders.Freight," +
                "Customers.CompanyName, Customers.City, " +
                "(Employees.FirstName + Space(1) + Employees.LastName) As Salesperson " +
                "FROM ((Orders " +
                "INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID) " +
                "INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID) " +
                "WHERE Orders.OrderID = " + "10655";

            OleDbConnection cnn = new OleDbConnection(strCon);
            OleDbCommand cmdOrder = new OleDbCommand(InvSql, cnn);
            cnn.Open();

            OleDbDataReader rdrOrder = cmdOrder.ExecuteReader();

            // Get CompanyName, City, Salesperson, OrderID, OrderDate and Freight
            rdrOrder.Read();
            CustomerName = rdrOrder["CompanyName"].ToString();
            CustomerCity = rdrOrder["City"].ToString();
            SellerName = rdrOrder["Salesperson"].ToString();
            SaleID = rdrOrder["OrderID"].ToString();
            System.DateTime dtOrder = Convert.ToDateTime(rdrOrder["OrderDate"]);
            SaleDate = dtOrder.ToShortDateString();
            SaleFreight = Convert.ToDecimal(rdrOrder["Freight"]);
            // Get invoice total
            GetInvoiceTotal();

            rdrOrder.Close();
            cnn.Close();
        }

在此处输入图像描述

string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Amrit\\Desktop\\Northwind.mdb ;Persist Security Info=False;";
4

3 回答 3

5

您需要分配strCon给正确的连接字符串。这个变量的值很可能是nullempty

例如:

strCon = "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;";
OleDbConnection cnn = new OleDbConnection(strCon);

您可以在以下位置找到所需的连接字符串:

http://connectionstrings.com/

于 2013-02-22T13:22:52.013 回答
0

如果您从其他地方复制此代码,那么您错过了strCon定义的行。它应该是这样的......

string strCon = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"
于 2013-02-22T13:26:57.057 回答
0

请停止像 MVP 一样猜测这个......,

我可以用一个强点重现,如果发生异常,如果你已经回滚了你的事务,并尝试继续进一步的数据检索,Connection is Closed,所以错误。

如果您想了解有关您的错误的更多详细信息,请在 VS IDE 中,保持异常打开。(调试->异常,检查异常...)您会看到导致此错误的脏代码!

水工

于 2013-07-22T12:18:14.627 回答