日期:2014-05-17  浏览次数:20404 次

救救我吧,实在搞不定,这方法哪错了?
C# code

   var tb = SQLHelper.GetTable("select * from bjx_zt");



C# code

    /// <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;
    }


这怎么老返回空呢,
我表里有内容, 数据库链接也没错,求救大牛....

------解决方案--------------------
SqlConnection conn = new SqlConnection(sqlConnectionString);
 
conn.Open();
 
SqlCommand cmd = new SqlCommand();
 
cmd.Connection = conn;
 
cmd.CommandType = CommandType.Text;
 
cmd.CommandText = commandText;
 
SqlDataAdapter da = new SqlDataAdapter(cmd); 
 
DataSet ds = new DataSet();
 
try
 
{
 
da.Fill(ds);
 
}

------解决方案--------------------
C# code

    /// <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;
}