操作数据库可以分这么三种,第一种,直接写硬SQL代码,不要参数,第二种,直接写硬代码,要参数,第三种,调用存储过程。
我们以一个登录模块为例,现在页面有两文本框,一按纽,实现验证用户名密码的功能。第一种方法主要代码如下:
SqlConnection conn =new SqlConnection ("server=;database=news2;uid=sa;pwd=");
conn.Open();
SqlCommand cmd=new SqlCommand();
cmd.CommandText="select count(*)from users where name='"+this.TextBox1.Text+"'and pwd='"+this.TextBox2.Text+"'";
cmd.Connection=conn;
int i=(int)cmd.ExecuteScalar();
Response.Write(i.ToString());
if(i==1)
{
Response.Redirect("add.aspx");}
else
{
Label1.Text="error!"
}
第二种途径
SqlConnection conn =new SqlConnection("server=;database=news;uid=sa;pwd=");
conn.Open();//打开数据库
SqlCommand cmd=new SqlCommand();//