using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
我看某些sqlhelper类写的执行sql语句返回数据都是返回DataSet,而使用时,还有取它的tables[0],其实把DataSet换成DataTable,如下,不行吗?
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dt = new DataTable();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(dt, "dt");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
我看某些sqlhelper类写的执行sql语句返回数据都是返回DataSet,而使用时,还有取它的tables[0],其实把DataSet换成DataTable,如下,不行吗?
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dt = new DataTable();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(dt, "dt");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
只不过,DataSet有Update方法,DataTable有吗?
{
DataSet ds = new DataSet();
try
{
SqlDataAdapter da= new SqlDataAdapter(SQLString, connection);
da.Fill(ds);
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
return ds.Tables[0]; //这里返回DataTable
}
速度 DataReader > (DataReader + IList) >DataSet
泛型使用比较多
只是个人认为如果用DataSet的话比较好用点。SQLHelper一般都是作通用一点的。。你如果只局限于DataTable,很多操作会受到限制,比如说Update,父子关系之类的东西不就用不了了么?