public static bool executescala(string sqlstr)
{
int count = 0;
SqlCommand cmd = new SqlCommand(sqlstr);
using (SqlConnection con = new SqlConnection(dbconnString))
{
createConnection(con); //打开连接
try
{
count = Convert.ToInt32(cmd.ExecuteScalar());
}
catch (Exception ex)
{
throw ex;
}
finally
{
closecon(con);
}
if (count > 0)
{
return true;
}
else
{
return false;
}
}
}请问这部分代码可有什么不妥的地方吗??
{
int count = 0;
SqlCommand cmd = new SqlCommand(sqlstr);
using (SqlConnection con = new SqlConnection(dbconnString))
{
createConnection(con); //打开连接
try
{
count = Convert.ToInt32(cmd.ExecuteScalar());
}
catch (Exception ex)
{
throw ex;
}
finally
{
closecon(con);
}
if (count > 0)
{
return true;
}
else
{
return false;
}
}
}请问这部分代码可有什么不妥的地方吗??
另外closecon(con); 画蛇添足,因为前面用的是USING,用完就释放了!
DataAccess的代码,满大街都是.
刚开始,可以去用一下:SqlHelper,它的源代码很多地方都能找到.
SqlCommand的Connection对象没有赋值.
{
int count = 0;
try
{
using (SqlConnection con = new SqlConnection(dbconnString))
{
con.Open();
SqlCommand cmd = new SqlCommand(sqlstr,con);
count = Convert.ToInt32(cmd.ExecuteScalar());
}
}
catch (Exception ex)
{ } return count > 0;
}