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;

Recommended Answers

All 3 Replies

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"]);
}

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

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

dataTableView1.DataSource = ds;
dataTableView1.DataMember = "Table1";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.