![Ado.NET/ActiveX <wbr>Data <wbr>Object.NET Ado.NET/ActiveX <wbr>Data <wbr>Object.NET](https://i-blog.csdnimg.cn/blog_migrate/2f85339f72d3f3c03279dc276b01b632.jpeg)
数据提供者包含4个主要组件:
Connection:用于连接到数据库或其它数据源。
Command:用于在数据库中检索、编辑、删除、或插入数据。
DataReader:从数据源提供数据流。这些数据只读(不可修改),我们只能向前经过这些数据。
-------------------------------------------------------------------------------------
DataReader对象没有公用的构造函数,所以不能使用DataReader对象的构造函数来创建一个DataReader对象,只能用Command对象中的ExecuteReader方法来创建一个DataReader对象
---------------------------------------------------------------------------------------
DataAdapter:用于将数据源中的数据填充到DataSet中,并将在DataSet中数据的变化返回到数据库中。可以将数据适配器看作是DataSet与数据库之间的桥梁。
DataSet :数据集,相当于驻留在内存中的数据库
使用Ado.NET实现以下功能:
private void Form1_Load(object sender, System.EventArgs e)
{
bind();
}
//查找用户信息
void bind()
{
SqlConnection conn=new SqlConnection("server=.;database=DBForADO;uid=sa;pwd=;");
SqlDataAdapter sda=new SqlDataAdapter("select * from login",conn);
DataSet ds=new DataSet();
sda.Fill(ds,0,0,"login");
this.dataGrid1.DataSource=ds.Tables["login"];
}
//Sql语句增加 =====SqlDataAdapter与DataSet组合
private void button1_Click(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection("server=.;database=DBForADO;uid=sa;pwd=;");
// string username=this.textBox1.Text;
// string userpwd=this.textBox2.Text;
SqlDataAdapter sda=new SqlDataAdapter("insert into login(uname,upwd) values('"+this.textBox1.Text+"','"+this.textBox2.Text+"')",conn);
DataSet ds=new DataSet();
sda.Fill(ds,0,0,"login");
bind();
}
//存储过程增加
private void button5_Click(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection("server=.;database=DBForADO;uid=sa;pwd=;");
SqlDataAdapter sda=new SqlDataAdapter("SqlAndProc_Login_Insert",conn);
sda.SelectCommand.CommandType=CommandType.StoredProcedure;
sda.SelectCommand.Parameters.Add("@name",this.textBox1.Text);
sda.SelectCommand.Parameters.Add("@pwd",this.textBox2.Text);
DataSet ds=new DataSet();
sda.Fill(ds,0,0,"login");
bind();
}
//Sql语句删除=====SqlDataAdapter与DataSet组合
private void button2_Click(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection("server=.;database=DBForADO;uid=sa;pwd=;");
SqlDataAdapter sda=new SqlDataAdapter("delete from login where uname='"+this.textBox1.Text+"'",conn);
DataSet ds=new DataSet();
sda.Fill(ds,"login");
this.dataGrid1.DataSource=ds.Tables["login"];
bind();
}
//存储过程删除(删除输入的用户名的记录)
private void button6_Click(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection("server=.;database=DBForADO;uid=sa;pwd=;");
SqlDataAdapter sda=new SqlDataAdapter("SqlAndProc_Login_Del",conn);
sda.SelectCommand.CommandType=CommandType.StoredProcedure;
sda.SelectCommand.Parameters.Add("@name",this.textBox1.Text);
DataSet ds=new DataSet();
sda.Fill(ds,0,0,"login");
bind();
}
//Sql语句查找=====SqlDataAdapter与DataSet组合
private void button3_Click(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection("server=.;database=DBForADO;uid=sa;pwd=;");
SqlDataAdapter sda=new SqlDataAdapter("select * from login where uname='"+this.textBox1.Text+"'",conn);
DataSet ds=new DataSet();
sda.Fill(ds,"login");
this.dataGrid1.DataSource=ds.Tables["login"];
}
//存储过程查找
private void button7_Click(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection("server=.;database=DBForADO;uid=sa;pwd=;");
SqlDataAdapter sda=new SqlDataAdapter("SqlAndProc_Login_Select",conn);
sda.SelectCommand.CommandType=CommandType.StoredProcedure;
sda.SelectCommand.Parameters.Add("@name",this.textBox1.Text);
DataSet ds=new DataSet();
sda.Fill(ds,"login");
this.dataGrid1.DataSource=ds.Tables["login"];
}
//Sql语句修改
private void button4_Click(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection("server=.;database=DBForADO;uid=sa;pwd=;");
conn.Open();
SqlCommand cmd=new SqlCommand("update login set upwd='"+this.textBox2.Text+"' where uname='"+this.textBox1.Text+"'",conn);
int i=cmd.ExecuteNonQuery();
if(i==1)
{
MessageBox.Show("密码修改成功!!!");
}
conn.Close();
bind();
}
//存储过程修改
private void button8_Click(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection("server=.;database=DBForADO;uid=sa;pwd=;");
SqlDataAdapter sda=new SqlDataAdapter("SqlAndProc_Login_Update",conn);
sda.SelectCommand.CommandType=CommandType.StoredProcedure;
sda.SelectCommand.Parameters.Add("@name",this.textBox1.Text);
sda.SelectCommand.Parameters.Add("@pwd",this.textBox2.Text);
DataSet ds=new DataSet();
sda.Fill(ds,0,0,"login");
bind();
}