1.首先添加引用Microsoft ADO Ext. 2.8 for DDL and Security
2.在.vb文件中引用:Imports ADOX
3.为保存access数据库的文件夹赋予写的权利:
Code PublicSub CreateMdb()Sub CreateMdb() Dim dbName AsString= HttpContext.Current.Server.MapPath("~\IMP_EXP_FILES\INTERFACE\DMS\DMS_LOT_PREFIXyyQn.mdb") Dim cat As ADOX.CatalogClass =New ADOX.CatalogClass() cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ dbName +";Jet OLEDB:Engine Type=5") Dim objTable As ADOX.TableClass =New ADOX.TableClass() objTable.ParentCatalog = cat objTable.Name ="LOT_TABLE" Dim col1 As ADOX.ColumnClass =New ADOX.ColumnClass() col1.ParentCatalog = cat col1.Name ="REGION_CODE" col1.Properties("Jet OLEDB:Allow Zero Length").Value =False objTable.Columns.Append(col1, ADOX.DataTypeEnum.adVarChar, 1) Dim col2 As ADOX.ColumnClass =New ADOX.ColumnClass() col2.ParentCatalog = cat col2.Name ="LOT_PREFIX" col2.Properties("Jet OLEDB:Allow Zero Length").Value =False objTable.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 7) Dim col3 As ADOX.ColumnClass =New ADOX.ColumnClass() col3.ParentCatalog = cat col3.Name ="LOT_PREFIX_CHIN" col3.Properties("Jet OLEDB:Allow Zero Length").Value =False objTable.Columns.Append(col3, ADOX.DataTypeEnum.adVarChar, 15) Dim col4 As ADOX.ColumnClass =New ADOX.ColumnClass() col4.ParentCatalog = cat col4.Name ="LOT_PREFIX_ENG" col4.Properties("Jet OLEDB:Allow Zero Length").Value =False objTable.Columns.Append(col4, ADOX.DataTypeEnum.adVarChar, 50) Dim objKey As ADOX.Key =New ADOX.Key() objKey.Name ="PrimaryKey" objKey.Type = KeyTypeEnum.adKeyPrimary objKey.Columns.Append("REGION_CODE") objKey.Columns.Append("LOT_PREFIX") objTable.Keys.Append(objKey) cat.Tables.Append(objTable) objTable =Nothing cat =Nothing End Sub
Code PublicSub InsertDataToAccess()Sub InsertDataToAccess(ByVal sSql AsString) Dim ds As DataSet = oSqlHelper.ExecuteDataSet(CommandType.Text, sSql, Nothing) Dim ole AsString=String.Empty Dim SourcePath AsString= Server.MapPath("~\IMP_EXP_FILES\INTERFACE\DMS\DMS_LOT_PREFIXyyQn.mdb") Dim sOleConnectionString AsString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ SourcePath +";Persist Security Info=true; " Dim oOlecon As OleDbConnection =New OleDbConnection(sOleConnectionString) oOlecon.Open() Dim oOleDbCommand As OleDbCommand =New OleDbCommand("DELETE FROM LOT_TABLE", oOlecon) oOleDbCommand.ExecuteNonQuery() Dim oOleDbDataAdapter As OleDbDataAdapter =New OleDbDataAdapter(sSql, oOlecon) Dim dtDataSet As DataSet =New DataSet() oOleDbDataAdapter.Fill(dtDataSet, "LOT_TABLE") For i AsInteger=0To ds.Tables(0).Rows.Count -1 Dim drDataRow As DataRow = dtDataSet.Tables("LOT_TABLE").NewRow() drDataRow("REGION_CODE") = ds.Tables(0).Rows(i)("REGION_CODE") drDataRow("LOT_PREFIX") = ds.Tables(0).Rows(i)("LOT_PREFIX") drDataRow("LOT_PREFIX_CHIN") = ds.Tables(0).Rows(i)("LOT_PREFIX_CHIN") drDataRow("LOT_PREFIX_ENG") = ds.Tables(0).Rows(i)("LOT_PREFIX_ENG") dtDataSet.Tables(0).Rows.Add(drDataRow) Next Dim builderSYYQXX As OleDbCommandBuilder =New OleDbCommandBuilder(oOleDbDataAdapter) oOleDbDataAdapter.Update(dtDataSet, "LOT_TABLE") dtDataSet.Dispose() oOlecon.Close() End Sub