C# OLEDB数据库

读取本地数据库:                    

 string strConn;

                //xls的时候用Microsoft.Jet.OLEDB.4.0

                //xlsx的时候用Microsoft.Ace.OleDb.12.0

                strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\FctTest\VI\DataBase\login.mdb;Persist Security Info=False";

//连接数据库:

                OleDbConnection OleConn = new OleDbConnection(strConn);

                OleConn.Open();

//查找数据库指令

                string sql = "select * from login where name='admin'";

                OleDbCommand aCommand = new OleDbCommand(sql, OleConn);

//读取数据库

                OleDbDataReader reader = aCommand.ExecuteReader();

                //OleDbDataReader遍历读取

                while (reader.Read())

                {

                    var t1 = reader["myPassword"].ToString();

                    textBox1.Text = t1;

                }

写入数据库:

string strConn;

//xls的时候用Microsoft.Jet.OLEDB.4.0

//xlsx的时候用Microsoft.Ace.OleDb.12.0

strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\FctTest\VI\DataBase\login.mdb;Persist Security Info=False";

OleDbConnection OleConn = new OleDbConnection(strConn);

OleConn.Open();

//插入操作:login为表名

//INSERT INTO <表名> (<字段列表>) values (<值列表>)

string sql = "Insert into login ([name],[myPassword])values('1','2')";

OleDbCommand aCommand = new OleDbCommand(sql, OleConn);

aCommand.ExecuteNonQuery();

 

更新数据:

string strConn;

//xls的时候用Microsoft.Jet.OLEDB.4.0

//xlsx的时候用Microsoft.Ace.OleDb.12.0

strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\FctTest\VI\DataBase\login.mdb;Persist Security Info=False";

OleDbConnection OleConn = new OleDbConnection(strConn);

OleConn.Open();

 

//UPDATE table SET newvalue WHERE criteria

 

string sql = "UPDATE login SET [myPassword]='123' WHERE [name]='admin'";

 

OleDbCommand aCommand = new OleDbCommand(sql, OleConn);

aCommand.ExecuteNonQuery();

连接带有密码的数据库:

strConn = @"Provider=Microsoft.Ace.OleDb.12.0;Data Source=C:\Users\Administrator\Desktop\login.accdb;Jet OLEDB:Database Password=123";

Jet OLEDB:Database Password=123"; 密码为:123

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页