public static object ExecuteScalar(string sql, SqlParameter[] parameters)
{
if (parameters != null)
{
foreach (SqlParameter p in parameters)
{
if (p.Value == null)// || p.Value.ToString().Trim() == "System.Byte[]")
p.Value = DBNull.Value;
}
}
using (SqlConnection con = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
if (parameters != null)
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(parameters);
}
else
{
cmd.CommandType = CommandType.Text;
}
cmd.CommandText = sql;
try
{
con.Open();
return cmd.ExecuteScalar();
}
catch
{
return string.Empty;
}
}
}
}
你具體是什麼問題,這代碼用來執行一個sql返回第一行第一列