2016-12-15 3 views
0

Dies ist, was ich bisher:Ärger ein Datatable von MS SQL-Tabelle füllt

static void Main(string[] args) 
     { 
      DataTable t = new DataTable(); 

      string connetionString = null; 
      SqlConnection cnn ; 
      connetionString = "Data Source=local.url;Initial Catalog=databasename;User ID=username;Password=password"; 

      cnn = new SqlConnection(connetionString); 

      string sql = "SELECT * FROM shiplabels"; 
      SqlDataAdapter a = new SqlDataAdapter(sql, cnn); 

      try 
      { 
       cnn.Open(); 
       a.Fill(t); 
       cnn.Close(); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine ("Can not open connection ! "); 
      } 
     } 

I dieser Microsoft DB verbinden möchten und ziehen Daten aus. Ich habe Probleme damit, das zum Laufen zu bringen! Wenn ich diesen Code verwende, hat Datentabelle t 0 Zeilen, wo es mit ein paar hundert zurückkommen sollte. Mir fehlt eindeutig etwas Einfaches hier?

+0

Dieses Ergebnis würde stark darauf hindeuten, dass es tatsächlich Null Zeilen in der 'shiplabels' Tabelle gibt. Vielleicht haben Sie beim Debugging etwas übersehen? – David

+0

Sie falsch geschrieben connectionString ... nur zu sagen. – SqlZim

+0

Warum verwenden Sie kein ORM? – adamshakhabov

Antwort

0
 DataTable dt = new DataTable(); 
     SqlDataAdapter sqlAdtp = new SqlDataAdapter(); 
     string connectionString = "Data Source=local.url;Initial Catalog=databasename;User ID=username;Password=password"; 
     string sql = "SELECT * FROM shiplabels"; 

     using (SqlConnection conn = new SqlConnection(connectionString)) 
     { 
      using (SqlCommand cmd = new SqlCommand(sql, conn)) 
      { 
       cmd.CommandType = CommandType.Text;     

       try 
       { 
        sqlAdtp.SelectCommand = cmd; 
        sqlAdtp.Fill(dt); 
       } 
       catch (Exception ex) 
       { 

       } 
      } 
     } 

Zuerst müssen Sie die Verbindung nicht öffnen, wenn Sie SqlDataAdapter verwenden. Sie haben auch den CommandType vergessen.

+0

Der Standard-CommandType ist ' Text' – Plutonix

+0

Ja, Sie haben Recht. Habe das vergessen. –

0

Dies sollte für Sie gut funktionieren.

using System; 
using System.Windows.Forms; 
using System.Data; 
using System.Data.SqlClient; 

namespace WindowsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      string connetionString = null; 
      SqlConnection sqlCnn ; 
      SqlCommand sqlCmd ; 
      SqlDataAdapter adapter = new SqlDataAdapter(); 
      DataSet ds = new DataSet(); 
      int i = 0; 
      string sql = null; 

      connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"; 
      sql = "Select * from product"; 

      sqlCnn = new SqlConnection(connetionString); 
      try 
      { 
       sqlCnn.Open(); 
       sqlCmd = new SqlCommand(sql, sqlCnn); 
       adapter.SelectCommand = sqlCmd; 
       adapter.Fill(ds); 
       for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) 
       { 
        MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " -- " + ds.Tables[0].Rows[i].ItemArray[1]); 
       } 
       adapter.Dispose(); 
       sqlCmd.Dispose(); 
       sqlCnn.Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Can not open connection ! "); 
      } 
     } 
    } 
}