【毕设】关于ADO.NET的一些问题
查询数据库:
using(SqlConnection conn = new SqlConnection(str)) { string comStr="INSERT INTO Product VALUES(...)"; SqlCommand com = new SqlCommand(comStr, conn); com.ExecuteNonQuery();//返回执行行数 }
获取数据库数据到DataSet:
using(SqlConnection conn= new SqlConnection(str)) { DataSet ds = new DataSet(); SqlCommand com = new SqlCommand(comStr, conn); SqlDataAdapter sda = new SqlDataAdapter(com);//SqlDataAdapter另有重载方法 sda.Fill(ds); } DataGridView1.DataSource = ds.table["tableName"];//DataSet包含DataTable
获取数据库数据到DataTable:
using(SqlConnection conn= new SqlConnection(str)) { DataTable dt = new DataTable(); qlDataAdapter sda = new SqlDataAdapter(str, connection);//可不用SqlCommand sda.Fill(dt); }
浙公网安备 33010602011771号