SQL连接失败

时间:2014-03-02 22:05:17

标签: c# sql .net oracle datagridview

我正在尝试执行以下代码(某些位已经过编辑以确保安全性)

private void ServiceMainScreen_Load(object sender, EventArgs e)
    {
        //DataTable stuff = new DataTable();
        //stuff.Select("select "WorkOrder"."WorkOrderID", "Customer"."FirstName", "Vehicles"."Model", "WorkOrder"."State" from "WorkOrder", "Customer", "Vehicles" WHERE "WorkOrder"."VIN" = "Vehicles"."VIN" AND "Vehicles"."CustomerID" = "Customer"."CustomerID" AND "WorkOrder"."State" = 'In Progress';
        try
        {
            string commands = "select \"WorkOrder\".\"WorkOrderID\", \"Customer\".\"FirstName\", \"Vehicles\".\"Model\", \"WorkOrder\".\"State\"  from \"WorkOrder\", \"Customer\", \"Vehicles\" WHERE \"WorkOrder\".\"VIN\" = \"Vehicles\".\"VIN\" AND \"Vehicles\".\"CustomerID\" = \"Customer\".\"CustomerID\" AND \"WorkOrder\".\"State\" = 'In Progress'";

            using (SqlConnection conn = new SqlConnection("Data Source=rowkir0911;Persist Security Info=True;User ID=xxxx;Password=xxxx;"))
            using (SqlCommand command = new SqlCommand(commands, conn))
            {
                command.Connection = conn;
                conn.Open();
                using (SqlDataAdapter adapter = new SqlDataAdapter())
                {
                    adapter.SelectCommand = command;
                    DataTable table = new DataTable();
                    adapter.Fill(table);
                    dgvDisplayWOs.DataSource = table;
                    dgvDisplayWOs.AutoGenerateColumns = true;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

但是,它似乎无法找到数据库。我可以访问数据库的Get Started / Application Express以及SQL命令行,但我需要程序来生成表。我在Visual Studio 2010上使用Winforms和C#。

3 个答案:

答案 0 :(得分:0)

在执行任何命令之前,必须通过调用conn.Open();来打开SQL连接。

答案 1 :(得分:0)

您的连接字符串或查询都未指定数据库名称。 您可以修改连接字符串

using (SqlConnection conn = new SqlConnection("Data Source=rowkir0911;Persist Security Info=True;initial catalog=xxxx;User ID=xxxx;Password=xxxx;"))

或者您可以修改查询以使用合格的表名,如[DB]。[Schema]。[Table]

答案 2 :(得分:0)

我相信您正在尝试连接到Oracle。但是您正在使用System.Data.SqlClient中用于连接SQL Server数据库的类。

使用ODP.Net代替。您可以在Oracle.DataAccess.Client

下找到所有必需的类

<强>更新

您需要添加对Oracle.DataAccess.dll程序集的引用。

以下是您可以尝试的代码 -

   using Oracle.DataAccess.Client;
   .
   .
   .
   try
    {
        string commands = "select \"WorkOrder\".\"WorkOrderID\", \"Customer\".\"FirstName\", \"Vehicles\".\"Model\", \"WorkOrder\".\"State\"  from \"WorkOrder\", \"Customer\", \"Vehicles\" WHERE \"WorkOrder\".\"VIN\" = \"Vehicles\".\"VIN\" AND \"Vehicles\".\"CustomerID\" = \"Customer\".\"CustomerID\" AND \"WorkOrder\".\"State\" = 'In Progress'";

        using (OracleConnection conn = new OracleConnection("Data Source=rowkir0911;Persist Security Info=True;User ID=xxxx;Password=xxxx;"))
        using (OracleCommand command = new OracleCommand(commands, conn))
        {
            command.Connection = conn;
            conn.Open();
            using (OracleDataAdapter adapter = new OracleDataAdapter())
            {
                adapter.SelectCommand = command;
                DataTable table = new DataTable();
                adapter.Fill(table);
                dgvDisplayWOs.DataSource = table;
                dgvDisplayWOs.AutoGenerateColumns = true;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }