C#中,求一个返回类型为DataTable,并用SqlParameter传参的SqlHelper_百度...
发布网友
发布时间:2024-10-21 03:39
我来回答
共1个回答
热心网友
时间:2024-10-22 18:32
Command和Connection没有关联起来
你的GetConn()返回的是什么?是connectionString还是 SqlConnection
如果是connectionString
public static DataTable getDateTable(string sqlString, SqlParameter[] parms)
{
GetConn();
SqlConnection conn = new SqlConnection(GetConn());
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(sqlString,conn);
if (parms != null)
{
foreach (SqlParameter p in parms)
{
cmd.Parameters.Add(p);
}
}
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
cmd.Parameters.Clear();
}
return dt;
}
如果是SqlConnection
public static DataTable getDateTable(string sqlString, SqlParameter[] parms)
{
SqlConnection conn= GetConn();
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(sqlString,conn);
if (parms != null)
{
foreach (SqlParameter p in parms)
{
cmd.Parameters.Add(p);
}
}
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
cmd.Parameters.Clear();
}
return dt;
}
热心网友
时间:2024-10-22 18:36
Command和Connection没有关联起来
你的GetConn()返回的是什么?是connectionString还是 SqlConnection
如果是connectionString
public static DataTable getDateTable(string sqlString, SqlParameter[] parms)
{
GetConn();
SqlConnection conn = new SqlConnection(GetConn());
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(sqlString,conn);
if (parms != null)
{
foreach (SqlParameter p in parms)
{
cmd.Parameters.Add(p);
}
}
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
cmd.Parameters.Clear();
}
return dt;
}
如果是SqlConnection
public static DataTable getDateTable(string sqlString, SqlParameter[] parms)
{
SqlConnection conn= GetConn();
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(sqlString,conn);
if (parms != null)
{
foreach (SqlParameter p in parms)
{
cmd.Parameters.Add(p);
}
}
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
cmd.Parameters.Clear();
}
return dt;
}