關於表的操作:
新建表:
create table [表名]
(
[自動編號字段] int IDENTITY (1,1) PRIMARY KEY ,
[字段1] nVarChar(50) default 默認值 null ,
[字段2] ntext null ,
[字段3] datetime,
[字段4] money null ,
[字段5] int default 0,
[字段6] Decimal (12,4) default 0,
[字段7] image null ,
)
刪除表:
Drop table [表名]
插入數據:
INSERT INTO [表名] (字段1,字段2) VALUES (100,abc.NET)
刪除數據:
DELETE FROM [表名] WHERE [字段名]>100
更新數據:
UPDATE [表名] SET [字段1] = 200,[字段2] = abc.NET WHERE [字段三] = HAIWA
新增字段:
ALTER TABLE [表名] ADD [字段名] NVARCHAR (50) NULL
刪除字段:
ALTER TABLE [表名] DROP COLUMN [字段名]
修改字段:
ALTER TABLE [表名] ALTER COLUMN [字段名] NVARCHAR (50) NULL
重命名表:(Access 重命名表,請參考文章:在Access數據庫中重命名表)
sp_rename 表名, 新表名, OBJECT
新建約束:
ALTER TABLE [表名] ADD CONSTRAINT 約束名 CHECK ([約束字段] <= 2000-1-1)
刪除約束:
ALTER TABLE [表名] DROP CONSTRAINT 約束名
新建默認值
ALTER TABLE [表名] ADD CONSTRAINT 默認值名 DEFAULT abc.NET FOR [字段名]
刪除默認值
ALTER TABLE [表名] DROP CONSTRAINT 默認值名
以上部分轉載自 作者:PeterXu 來源:Blog.CSDN Blog: http://blog.csdn.net/peterreg/
版權聲明:原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章原始出版、作者信息和本聲明,否則將追究法律責任。本文地址:http://blog.csdn.net/peterreg/archive/2008/05.aspx
--------------------------------------華麗的分割線-----------------------------------------
關於Access表的重命名:
數據量不大的時候可以選擇:
select * into newtable name from oldTable;
drop table oldTableName;
---------------------------華麗的分割線-----------------------------------------
數據類型
access數據類型大全
ALTER TABLE tb ALTER COLUMN aa Byte 數字[字節]
ALTER TABLE tb ALTER COLUMN aa Long 數字[長整型]
ALTER TABLE tb ALTER COLUMN aa Short 數字[整型]
ALTER TABLE tb ALTER COLUMN aa Single 數字[單精度]
ALTER TABLE tb ALTER COLUMN aa Double 數字[雙精度]
ALTER TABLE tb ALTER COLUMN aa Currency 貨幣
ALTER TABLE tb ALTER COLUMN aa Char 文本
ALTER TABLE tb ALTER COLUMN aa Text(n) 文本,其中n表示字段大小
ALTER TABLE tb ALTER COLUMN aa Binary 二進制
ALTER TABLE tb ALTER COLUMN aa Counter 自動編號
ALTER TABLE tb ALTER COLUMN aa Memo 備注
ALTER TABLE tb ALTER COLUMN aa Time 日期/時間
ALTER TABLE tb ALTER COLUMN aa bit 是否
-------------------------------------華麗的分割線------------------------------------
C#將excel導入Access
首先將excel導出到Datatable
string excelConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + this.tbExcelPath.Text +
";Extended Properties=Excel 8.0;HDR=YES;IMEX=1; ";
string query = "SELECT * FROM ["+this.cbSheetName.SelectedItem.ToString()+"$]";
OleDbCommand oleCommand = new OleDbCommand(query, new OleDbConnection(excelConnectionStr));
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
DataSet myDataSet = new DataSet();
try
{
oleAdapter.Fill(myDataSet, this.tbNewDBTableName.Text);
}
catch (Exception exx)
{
MessageBox.Show(exx.Message);
}
myDataSet.Tables[0].TableName = this.tbNewDBTableName.Text;
C# 將Dataset或者DataTable導入Access
先將dataset或者datatable導出為xml,再導入,如果有需要 自行增加主鍵.
String strXML = Environment.GetEnvironmentVariable("Temp") + "//xmldata.xml/";
myDataSet.WriteXml(strXML,XmlWriteMode.WriteSchema);
Microsoft.Office.Interop.Access.Application accessApp = new Microsoft.Office.Interop.Access.ApplicationClass();
accessApp.OpenCurrentDatabase(this.tbDatabasePath.Text, false, "");
accessApp.ImportXML(strXML, AcImportXMLOption.acStructureAndData);
accessApp.CloseCurrentDatabase();
accessApp.Quit(AcQuitOption.acQuitSaveAll);
System.Runtime.InteropServices.Marshal.ReleaseComObject(accessApp);
accessApp=null;
GC.Collect();
System.IO.File.Delete(strXML);
-------------------------------------華麗的分割線------------------------------------
將dataset或者datatable保存到表中:
先刪除,再添加:
OleDbConnection oleConn = new OleDbConnection(connectStr);
String sql = "delete from " + curModelName + " where ID >0";
OleDbCommand cmd = new OleDbCommand(sql, oleConn);
try
{
if (oleConn.State != ConnectionState.Open)
{
oleConn.Open();
&n