日期:2014-05-17 浏览次数:20404 次
var tb = SQLHelper.GetTable("select * from bjx_zt");
/// <summary> /// 返回指定Sql语句的DataTable /// </summary> /// <param name="strSql">传入的Sql语句</param> /// <returns>DataTable</returns> public static DataTable GetTable(string strSql) { SqlDataAdapter da = new SqlDataAdapter(); DataTable datatable = new DataTable(); try { using (SqlConnection connection = new SqlConnection(StrConn)) { using (SqlCommand cmd = new SqlCommand(strSql, connection)) { try { connection.Open(); cmd.CommandType = CommandType.Text; da.SelectCommand = cmd; da.Fill(datatable); } catch { connection.Close(); } } } } catch { } return datatable; }
/// <summary> /// 返回指定Sql语句的DataTable /// </summary> /// <param name="strSql">传入的Sql语句</param> /// <returns>DataTable</returns> public static DataTable GetTable(string strSql) { DataTable datatable = new DataTable(); try { using (SqlConnection connection = new SqlConnection(StrConn)) { SqlDataAdapter adapter = new SqlDataAdapter(strSql,connection); DataSet ds = new DataSet(); adapter.Fill(ds); datatable = ds.Tables[0]; } } catch { } return ds; }
------解决方案--------------------
/// <summary>
/// 返回指定Sql语句的DataTable
/// </summary>
/// <param name="strSql">传入的Sql语句</param>
/// <returns>DataTable</returns>
public static DataTable GetTable(string strSql)
{
DataTable datatable = new DataTable();
try
{
using (SqlDataAdapter adapter = new SqlDataAdapter(strSql,StrConn))
{
DataSet ds = new DataSet();
adapter.Fill(ds);
datatable = ds.Tables[0];
}
}
catch { }
return ds;
}