首页 > 代码库 > C#access数据库操作

C#access数据库操作

比较凌乱,有时间在整理吧。

 

 1 Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data source=c:/D.mdb;Jet OLEDB:DataBase Password=123456!" 2  3  4  private void GetCon() 5        { 6        string strConnection="PRovider=Microsoft.Jet.OleDb.4.0;"; 7                strConnection+=@"Data Source=C:\BegaspNET\Northwind.mdb"; 8  9         OleDbConnection objConnection=new OleDbConnection(strConnection);10 11 12       objConnection.Open();13       objConnection.Close();14 15        }

 
以独占的方式打开

文件--信息--加密/解密

PRovider=Microsoft.Jet.OleDb.4.0;Jet OLEDB:DataBase Password=123456789;Data Source=D:\ToWife.mdb;1.set dbconnection=Server.CREATEOBJECT("ADODB.CONNECTION")DBPath = Server.MapPath("customer.mdb")dbconnection.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPathSQL="select * from auth where id="" & user_id &"""SET uplist=dbconnection.EXECUTE(SQL)2.set dbconnection=Server.CreateObject("ADODB.Connection")DBPath = Server.MapPath("customer.mdb")dbconnection.Open "provider=microsoft.jet.oledb.4.0;data source="&dbpathSQL="select * from auth where id="" & user_id &"""SET uplist=dbconnection.EXECUTE(SQL)3.DBPath = Server.MapPath("customer.mdb")set session("rs")=Server.CreateObject("ADODB.Recordset")" rs=Server.CreateObject("ADODB.Recordset")connstr="provider=microsoft.jet.oledb.4.0;data source="&dbpathSQL="select * from auth where id="" & user_id &"""session("rs").Open sql,connstr,1,3

连接access2007

 

1 OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=D:/数据库.accdb;Jet OLEDB:Database Password=123456;

 

 连接access2003

 

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin";Data Source=D:/数据库.mdb;Jet OLEDB:Database password=123456;")

建odbc源

set conn=server.createobject("Adodb.connection")conn.open "DSN=xxx;UID=;PWD=123456;Database=XXX" 

破解密码

 1  public static string GetMDBPassword(string file) 2         {                       3             // 未加密的MDB文件,0x42到0x61之间的每间隔一字节的数值 4             byte[] baseByte = { 0xbe, 0xec, 0x65, 0x9c, 0xfe, 0x28, 0x2b, 0x8a, 0x6c, 0x7b, 0xcd, 0xdf, 0x4f, 0x13, 0xf7, 0xb1, }; 5             byte flagByte = 0x0c; // 标志 0x62 处的数值 6             string password = ""; 7             try 8             { 9                 FileStream fs = File.OpenRead(file);10                 fs.Seek(0x14, SeekOrigin.Begin);11                 byte ver = (byte)fs.ReadByte();     // 取得access版本, 1为Access2000/2003, 0为Access9712                 fs.Seek(0x42, SeekOrigin.Begin);13                 byte[] bs = new byte[33];14                 if (fs.Read(bs, 0, 33) != 33) return "";15                 byte flag = (byte)(bs[32] ^ flagByte);16                 for (int i = 0; i < 16; i++)17                 {18                     byte b = (byte)(baseByte[i] ^ bs[i * 2]);19                     if (i % 2 == 0 && ver == 1) b ^= flag; //Access 2000/200320                     if (b > 0) password += (char)b;21                 }22             }23             catch { }24 25             return ( password.Equals( "" ) ? "没有密码!" : password );26         }

 

up

 1 private int up(string sql) 2         { 3             int index = 0; 4             OleDbConnection con = GetCon();             5             try 6             { 7                 if (con!=null) 8                 { 9                     con.Open();10                     OleDbCommand oc = new OleDbCommand(sql, con);11                     return oc.ExecuteNonQuery();12                 }13                 return 0;14             }15             catch (Exception)16             {17                 return 0;18             }19         }

 

select

 1  private DataTable select(string sql) 2         { 3             DataTable dt = new DataTable(); 4             OleDbConnection con = GetCon(); 5             try 6             { 7                 if (con!=null) 8                 { 9                     con.Open();10                     //用   OleDbDataAdapter   得到一个数据集11                     OleDbDataAdapter myCommand = new OleDbDataAdapter(sql,con);12                     DataSet myDataSet = new DataSet();13                     //把Dataset绑定books数据表14                     myCommand.Fill(myDataSet);15                     con.Close();16                     return myDataSet.Tables[0]; 17                 }18                 con.Close();19                 return null;20             }21             catch (Exception)22             {23                 con.Close();24                 return null;25             }26         }

 

主键的操作Function AddPrimaryKey()添加主键到[编号]字段Dim strSQL As StringstrSQL = "ALTER TABLE 表1 ADD CONSTRAINT PRIMARY_KEY " _& "PRIMARY KEY (编号)"CurrentProject.Connection.Execute strSQLEnd FunctionFunction DropPrimaryKey()删除主键Dim strSQL As StringstrSQL = "ALTER TABLE 表1 Drop CONSTRAINT PRIMARY_KEY "CurrentProject.Connection.Execute strSQLEnd Function

 

C#access数据库操作