OleDbCommand OleDbDataAdapter比较研究素材

转载地址:http://blog.sina.com.cn/s/blog_43eb83b901017fy6.html
致谢!
-------------------------------------------------------------------------------------------------------
为什么使用OleDbCommand时

OleDbConnection对象要先Open(),
然后再Close()
而使用OleDbDataAdapter时,
OleDbConnection不用Open(),也不用关闭

因为OleDbDataAdapter是自动打开和自动关闭的。
而OleDbCommand是需要手动打开的!

 

 

OleDbConnection con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=data.mdb");
con.Open();
OleDbCommand dc=new OleDbCommand("select name from us where id=2", con);
textBox1.Text=dc.ExecuteScalar().ToString();

OleDbConnection con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=data.mdb");
con.Open();
OleDbCommand dc=new OleDbCommand("insert into us (name) values (11)", con);
dc.ExecuteNonQuery();
con.Close();


string us=textBox2.Text;
string pa=textBox1.Text;
OleDbConnection con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=data.mdb"); 
OleDbCommand oldC=new OleDbCommand("insert into users (username, userpassword) values (@username, @userpassword)", con);
oldC.Parameters.Add("@username", us);
oldC.Parameters.Add("@userpassword", pa);
con.Open();
oldC.ExecuteNonQuery();
con.Close();
MessageBox.Show("Register successful!");
this.Dispose();

 

也有先有OleDbCommand封装select语句,再用OleDbCommand构建OleDbAdapter的

 
posted @ 2017-08-28 14:47  marvelousone  阅读(427)  评论(0编辑  收藏  举报