C#通过Ado.net对连接数据库并进行添加删除等常规操作的代码
如下资料是关于C#通过Ado.net对连接数据库并进行添加删除等常规操作的内容。
static string sqlcon = "server=.;database=;Integrated Security=true;";
public static void Add()
{
SqlConnection conn=new SqlConnection(sqlcon);
string sqlStr="insert into Boook(b_id,b_title,b_author,b_money)values(1,'ASP.NET','莫言',100)";
SqlCommand cmd=new SqlCommand(sqlStr,conn);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
if (i>0)
{
Console.Write("新增成功!");
}
else
{
Console.Write("新增失败!");
}
}
public static void Delete()
{
SqlConnection conn = new SqlConnection(sqlcon);
string sqlStr = "delete from book where b_id=1";
SqlCommand cmd = new SqlCommand(sqlStr,conn);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
if (i > 0)
{
Console.Write("删除成功!");
}
else
{
Console.Write("删除失败!");
}
}
private static void Update()
{
SqlConnection conn = new SqlConnection(sqlcon);
string sqlStr = "update Boook set b_id =2 where b_id=1";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
if (i > 0)
{
Console.Write("更新成功!");
}
else
{
Console.Write("更新失败!");
}
}
private static void selectSingle()
{
SqlConnection conn = new SqlConnection(sqlcon);
SqlCommand cmd = new SqlCommand(sqlStr, conn);
conn.Open();
object obj = cmd.ExecuteScalar();
conn.Close();
Console.Write(obj.ToString());
}
private static void dateReader()
{
SqlConnection conn = new SqlConnection(sqlcon);
SqlCommand cmd = new SqlCommand(sqlStr, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
{
Console.Write(dr[0].ToString() + '_' + dr[1].ToString()+'_'+dr["ID"].ToString());
}
}
else
{
Console.Write("无数据");
}
dr.Close();
conn.Close();
}
public static void QueryListAdapter()
{
SqlConnection con = new SqlConnection(sqlcon);
SqlDataAdapter da = new SqlDataAdapter(sqlStr,con);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
Console.WriteLine(dr[0].ToString() + "_" + dr["ID"].ToString());
}
}
public static void QueryListAdapter2()
{
SqlConnection con = new SqlConnection(sqlcon);
SqlDataAdapter da = new SqlDataAdapter(sqlStr, con);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
Console.WriteLine(dr[0].ToString() + "_" + dr["ID"].ToString());
}
}
public static void QuerListByProc()
{
SqlConnection conn = new SqlConnection(sqlcon);
SqlCommand cmd=new SqlCommand("usp_GetBookMyCateId",conn);
SqlParameter sp2 = new SqlParameter();
sp2.ParameterName = "@cateId";
sp2.SqlDbType = SqlDbType.Int;
sp2.Value = 2;
cmd.Parameters.Add(sp2);
SqlParameter sp = new SqlParameter("@cateId", 2);
cmd.Parameters.Add(sp);
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
da.Fill(dt);
foreach(DataRow dr in dt.Rows)
{
Console.WriteLine(dr[0].ToString() + "_" + dr["ID"].ToString());
}
}
private static void QueryListByProc2()
{
SqlConnection conn = new SqlConnection(sqlcon);
SqlCommand cmd = new SqlCommand("proGetPageData", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] paras ={
new SqlParameter("@pageSize",SqlDbType.Int,4)
};
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
Console.WriteLine(dr[0].ToString() + "_" + dr[1].ToString());
}
conn.Close();
}
private static void QuerListProc3()
{
SqlConnection conn = new SqlConnection(sqlcon);
SqlCommand cmd = new SqlCommand("proGetData2", conn);
SqlParameter[] paras ={
new SqlParameter("@pageIndex",SqlDbType.Int),
new SqlParameter("@pageSize",SqlDbType.Int),
new SqlParameter("@pageCount",SqlDbType.Int),
new SqlParameter("@rowCount",SqlDbType.Int)
};
paras[0].Value = 1;
paras[1].Value = 2;
paras[2].Direction = ParameterDirection.Output;
cmd.Parameters.AddRange(paras);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
Console.WriteLine(dr[0].ToString() + "_" + dr[1].ToString());
}
int pageCount=Convert.ToInt32(cmd.Parameters[2].Value);
Console.WriteLine("pageCount=" + pageCount + ",rowCount=" + rowCount);
}