SqlHelper.cs
时间:2021-07-01 10:21:17
帮助过:1人阅读
static class SqlHelper
{
public static object FromDbValue(
object value)
{
if (value ==
DBNull.Value)
{
return null;
}
else
{
return value;
}
}
public static object ToDbValue(
object value)
{
if (value ==
null)
{
return DBNull.Value;
}
else
{
return value;
}
}
private static string connStr = ConfigurationManager.ConnectionStrings[
"myconnstring"].ConnectionString;
public static int ExecuteNonQuery(
string sql,
params SqlParameter[] parameters)
{
using (SqlConnection conn =
new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd =
conn.CreateCommand())
{
cmd.CommandText =
sql;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteNonQuery();
}
}
}
public static object ExecuteScalar(
string sql,
params SqlParameter[] parameters)
{
using (SqlConnection conn =
new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd =
conn.CreateCommand())
{
cmd.CommandText =
sql;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteScalar();
}
}
}
public static DataTable ExecuteDataTable(
string sql,
params SqlParameter[] parameters)
{
using (SqlConnection conn =
new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd =
conn.CreateCommand())
{
cmd.CommandText =
sql;
cmd.Parameters.AddRange(parameters);
DataSet dataset =
new DataSet();
SqlDataAdapter apdater =
new SqlDataAdapter(cmd);
apdater.Fill(dataset);
return dataset.Tables[
0];
}
}
}
public static DataSet ExecuteDataSet(
string sql,
params SqlParameter[] parameters)
{
using (SqlConnection conn =
new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd =
conn.CreateCommand())
{
cmd.CommandText =
sql;
cmd.Parameters.AddRange(parameters);
DataSet dataset =
new DataSet();
SqlDataAdapter apdater =
new SqlDataAdapter(cmd);
apdater.Fill(dataset);
return dataset;
}
}
}
}
View Code
SqlHelper.cs
标签: