0

i dont know how to read from database SQL.
i try this :

string sql = "Select * from Table1 ";
            DataSet ds = new DataSet();
            string connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(ds);
            conn.Close();

how i call to the row to give me information or to write for row?(C#);

thanks;

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by nick.crane
0

In console application,

string sql = "Select * from Table1 ";
DataSet ds = new DataSet();
string connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";

SqlDataAdapter adapter = new SqlDataAdapter(sql,connStr);
adapter.Fill(ds,"tab1");

foreach(DataRow r in ds.Tables["tab1"].Rows) {
  // r[0] - columns ordinal - 1st column
  // or use r["column_name"]
  Console.WriteLine(r[0] + " " + r["column_name_here"]);
}
0

thank you but i search for windows forms .
and your code doesnt work.

0

Add a DataTableView to your form.
Then load your dataset as the DataSource.
E.G.

dataTableView1.DataSource = ds;
dataTableView1.DataMember = "Table1";
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.