以下两种DB 连接方式的差异在哪,那种方式更好!
//MSSQL DB 连接 1
SqlConnection myConn =null;
SqlDataAdapter dapt = null;
DataSet ds = new DataSet();
myConn = new SqlConnection(ConfigurationSettings.AppSettings[ "str2 "]);
myConn.Open();
dapt = new SqlDataAdapter( "select * from DSCMB;select * from DSCMA ", myConn);//差异
dapt.Fill(ds);
DataTable dt = ds.Tables[1]; //此句可以不用也行,而直接把 GridView2.DataSource 赋值为ds
GridView2.DataSource = dt;
GridView2.DataBind();
GridView2.AutoGenerateColumns = true;
// MSSQL DB 连接 2
SqlConnection myConn = null;
SqlDataAdapter dapt = null;
DataSet ds = new DataSet();
myConn = new SqlConnection(ConfigurationSettings.AppSettings[ "str2 "]);
myConn.Open();
string sqlst = "select * from DSCMB;select * from DSCMA ";
SqlCommand cmd1 = new SqlCommand(sqlst, myConn); //差异
SqlDataAdapter dap = new SqlDataAdapter(cmd1);//差异
dap.Fill(ds);
DataTable dt = ds.Tables[0]; //此句可以不用也行,而直接把 GridView2.DataSource 赋值为ds
GridView2.DataSource = dt;
GridView2.DataBind();
[解决办法]
这两个方法基本上没有 什么差异 只不过是第二个方法 把 SqlDataAdapter中执行的方法给叉分出来了。在执行速度上 应该看不出有差异。