C#创建通⽤增删改查代码//引⼊命名空间
using System.Data;
public static Dictionary<string, object> select(string sql)
{
string conn = Create();//⽅法见上⽂
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 0)
{
return null;
}
DataRow row = dt.Rows[0];
Dictionary<string, object> dic = new Dictionary<string, object>();
foreach (dataColumn item in dt.Columns)
{
dic.add(item.ColumnsName, row[item.ColumnsName])
}
return dic;
}
//查询多条数据
public static Datable Selects(string sql)
{
try
{
SqlConnection conn = Create();
SqlCommand com = new SqlCommand();
com.Connection = conn;
com.Command = sql;
SqlDataAdpter da = new SqlDataAdapter(com);
Dataset ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
catch (Exception ex)
{
throw ex;
}
}
基本的增删改查语句//执⾏⼀条SQL语句,增删改都可以⽤
public static int ExecutionSQl(string sql,Sqlconnection conn = null)
{
conn = conn = null ? Create() : conn;
try
{
conn.open();
SqlCommand com = new SqlCommand(sql, conn);
return com.ExecutionNonQuery();
}
catch (Exception ex)
{
console.write(ex.Message);
return -1;
}
finally
{
}
}
//保存修改带参数可⽤此⽅法
public static int ExecutionPar(string sql, SqlParameter[] sqlparameter) {
SqlConnection conn = Create();
SqlCommand com = null;
try
{
conn.open();
com = new SqlCommand();
com.Connection = conn;
com.CommandText = sql;
return com.ExexutionNonQuery();
}
catch (Execption ex)
{
console.WriteLine(ex, Message);
return -1;
}
finlly{
com.Parameter.Clear();
conn.Close();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论