SqlHelper封装类测试
Posted gzu_zb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SqlHelper封装类测试相关的知识,希望对你有一定的参考价值。
//更新 int flag = SqlHelper.ExecuteNonQuery("UPDATE [dbo].[user] SET [password] = @password WHERE [email protected]", new SqlParameter("@username", TextBox1.Text), new SqlParameter("@password", TextBox2.Text)); if (flag != 0) { Response.Write("更新成功!"); } else { Response.Write("更新失败!"); }
//插入 int flag = SqlHelper.ExecuteNonQuery("Insert into [dbo].[user] values (@username,@password)", new SqlParameter("@username", TextBox1.Text), new SqlParameter("@password", TextBox2.Text)); if (flag != 0) { Response.Write("插入成功!"); } else { Response.Write("插入失败!"); }
//删除 string sqlStrDel = "Delete from [dbo].[user] Where [email protected]"; int flag = SqlHelper.ExecuteNonQuery(sqlStrDel, new SqlParameter("@username", TextBox1.Text)); if (flag != 0) { Response.Write("删除成功!"); } else { Response.Write("删除失败!"); }
//1.执行增、删、改的方法:ExecuteNonQuery public static int ExecuteNonQuery(string sql, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(sql, con)) { if (pms != null) { cmd.Parameters.AddRange(pms); } con.Open(); try { return cmd.ExecuteNonQuery(); } catch (Exception) { return 0;//返回0表示操作失败 //throw; } } } }
查询
string sqlStr = "select * from [dbo].[user]"; SqlDataReader reader = SqlHelper.ExecuteReader(sqlStr); while (reader.Read()) { Response.Write(reader[0] + " " + reader[1] + "<br />"); }
string sqlStr = "select password from [user] where [email protected]"; SqlDataReader reader = SqlHelper.ExecuteReader(sqlStr,new SqlParameter("@username",TextBox1.Text)); while (reader.Read()) { Response.Write(reader[0]); }
//3.执行查询多行多列的数据的方法:ExecuteReader public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms) { SqlConnection con = new SqlConnection(connStr); using (SqlCommand cmd = new SqlCommand(sql, con)) { if (pms != null) { cmd.Parameters.AddRange(pms); } try { con.Open(); return cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch (Exception) { con.Close(); con.Dispose(); throw; } } }
以上是关于SqlHelper封装类测试的主要内容,如果未能解决你的问题,请参考以下文章