7

我在 SQL Server 2008 中有一个数据库并在WPF应用程序中连接它。我想从表中读取数据并显示在datagrid. 连接已成功创建,但是当我在网格中显示它时,它显示 db 错误(异常处理)。这就是我正在做的。在此先感谢。

  try
  {
       SqlConnection thisConnection = new SqlConnection(@"Server=(local);Database=Sample_db;Trusted_Connection=Yes;");
       thisConnection.Open();

       string Get_Data = "SELECT * FROM emp";     

       SqlCommand cmd = new SqlCommand(Get_Data);              
       SqlDataAdapter sda = new SqlDataAdapter(cmd);               
       DataTable dt = new DataTable("emp");
       sda.Fill(dt);
       MessageBox.Show("connected");
       //dataGrid1.ItemsSource = dt.DefaultView;           
  }
  catch
  {
       MessageBox.Show("db error");
  }

它显示connected当我评论该行时 sda.Fill(dt);

4

6 回答 6

11

SqlCommand不知道你打开了连接——它需要一个SqlConnection.

try
{
       SqlConnection thisConnection = new SqlConnection(@"Server=(local);Database=Sample_db;Trusted_Connection=Yes;");
       thisConnection.Open();

       string Get_Data = "SELECT * FROM emp";     

       SqlCommand cmd = thisConnection.CreateCommand();
       cmd.CommandText = Get_Data;

       SqlDataAdapter sda = new SqlDataAdapter(cmd);               
       DataTable dt = new DataTable("emp");
       sda.Fill(dt);

       dataGrid1.ItemsSource = dt.DefaultView;           
}
catch
{
       MessageBox.Show("db error");
}
于 2013-06-18T11:55:32.150 回答
0

我将此代码与 Oracle 一起使用,希望对您有所帮助。

首先添加引用Oracle.DataAccess然后使用 Oracle.DataAccess.Client 添加命名空间;

并使用以下代码

try
{
  string MyConString = "Data Source=localhost;User Id= yourusername;Password=yourpassword;";
  using (OracleConnection connection = new OracleConnection(MyConString))
  {
  connection.Open();
  sqldb1 = "select * from DEMO_CUSTOMERS;";
  using (OracleCommand cmdSe1 = new OracleCommand(sqldb1, connection))
  {
    DataTable dt = new DataTable();
    OracleDataAdapter da = new OracleDataAdapter(cmdSe1);
    da.Fill(dt);
    db1.ItemsSource = dt.DefaultView;
  }
  connection.Close();
}
catch (Exception ex)
{
  MessageBox.Show(ex.ToString());
}

XAML 代码:

<DockPanel>
    <DataGrid Margin="10.0" DockPanel.Dock="Left" Name="db1"  AutoGenerateColumns="True" >
    </DataGrid>
</DockPanel>
于 2015-02-08T08:22:44.650 回答
0

您没有为命令分配任何连接。您打开连接然后创建一个命令,但不要链接两者。

尝试类似:

SqlConnection conn = new SqlConnection(@"Server(local);Database=Sample_db;Trusted_Connection=Yes;");
conn.Open();

string sql= "SELECT * FROM emp";     

SqlCommand cmd = new SqlCommand(sql); 
cmd.Connection = conn;

SqlDataAdapter sda = new SqlDataAdapter(cmd);               
DataTable dt = new DataTable("emp");
sda.Fill(dt);
于 2013-06-18T11:55:08.137 回答
0

将连接对象分配给 SqlCommand 对象。

using (SqlConnection connection = new SqlConnection(
           connectionString))
{
    SqlCommand command = new SqlCommand();
    command.Connection = connection;
    command.CommandTimeout = 15;
    command.CommandType = CommandType.Text;
    command.CommandText = queryString;

    connection.Open();
   //Perfom desired Action

}
于 2013-06-18T11:55:56.027 回答
0

添加 thisConnection 作为第二个参数

Sqlcommand cmd = new SqlCommand(Get_Data, thisConnection)


try {
    string connectionstring = "@"
    Server = (local) Database = Sample_dbTrusted_Connection = Yes;
    "";
    SqlConnection thisConnection = new SqlConnection(connectionstring);
    thisConnection.Open();

    string Get_Data = "SELECT * FROM emp";

    SqlCommand cmd = new SqlCommand(Get_Data, thisConnection);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);`
    DataTable dt = new DataTable("emp");
    sda.Fill(dt);
    MessageBox.Show("connected");
    //dataGrid1.ItemsSource = dt.DefaultView;           
} catch {
    MessageBox.Show("db error");
}
于 2014-01-09T10:33:27.237 回答
0
public DataTable Execute(string cmd)
{
        bool networkUp = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
        if (networkUp)
        {
            try
            {
                    SqlConnection connection = new SqlConnection("ConnectionString");
                    SqlCommand command = new SqlCommand(cmd);
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        DataTable dt = new DataTable();
                        sda.SelectCommand = command;
                        command.Connection = connection;
                        connection.Open();
                        sda.Fill(dt);
                        connection.Close();
                        if (dt != null && dt.Columns.Count > 0)
                            return dt;
                        else
                            return null;
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }
        else
        {
            Console.WriteLine("Network is disconnect");
            return null;
        }
    return null;
}  

或者 :

  public void ExecuteNonQuery(string cmd)
        {
            bool networkUp = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
            if (networkUp)
            {
                try
                {
                  SqlConnection connection = new SqlConnection("ConnectionString");
                  SqlCommand command = new SqlCommand(cmd);
                  command.Connection = connection;
                  connection.Open();
                  command.ExecuteNonQuery();
                  connection.Close();
                }
                catch (Exception ex)
                {
                   Console.WriteLine(ex.Message);
                }
            }
        }
于 2016-11-07T08:47:02.917 回答