c#連線access資料庫
注意一般要寫成類mdbhelp:
using system;using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;
using system.data.oledb;
/// /// 連線資料庫
///
/// 資料庫名字
private void linkdb(string strconnection)
private void button1_click(object sender, eventargs e)
private void datagridview1_cellcontentclick(object sender, datagridviewcelleventargs e)
}}
增刪改查
增
string instert_str = "insert into 姓名表(姓名,年齡,性別) values ('" + "葫蘆娃" +"','"+"16"+"','"+"m"+"')oledbcommand mycommand = new oledbcommand(insert, dbconn);//執行命令
mycommand.executenonquery();//更新資料庫,返回受影響行數;可通過判斷其是否》0來判斷操作是否成功
注:可以用textbox.text 作為輸入值
注意 輸入語句的寫法 : '"+"str"+"'
刪
string s = "'" + textbox1.text + "
'";//
接受textbox1的字串
string delete = "
delete from student where studentname =
"+s;
//delete from 表名 where 欄位名='字段值';以上**執行後會將所有studentname為textbox中內容的行刪除
oledbcommand mycommand = new oledbcommand(delete, dbconn);//
執行命令
mycommand.executenonquery(); //更新資料庫,返回受影響行數;可通過判斷其是否》0來判斷操作是否成功
改
string s = "'" + textbox1.text + "'", x = "'" + textbox2.text + "'"; 接受textbox的字串string update = "update student set studentname=" + x + "where studentname = " + s;
//update 表名 set 欄位名='字段值' where 字段值='字段值';上一行**執行後將所有studentname中的s替換為x
oledbcommand mycommand = new oledbcommand(update, dbconn);//執行命令
mycommand.executenonquery();//更新資料庫,返回受影響行數;可通過判斷其是否》0來判斷操作是否成功
查
string s = "'" + textbox1.text + "'";//接受textbox1的字串string select = "select *from student where studentname = " + s;
//select *from 表名 where 欄位名='字段值';*表示全表,從全表中
oledbdataadapter inst = new oledbdataadapter(select, dbconn);//只匹配滿足條件的行
inst.fill(ds);
查詢後:
messagebox.show(ds.tables[0].rows[0]["studentno"].tostring());
//這行**可以展示ds中第一張表(tables[0])第一行(rows[0])["欄位名"]的資訊;在查詢後可以用這種方式輸出提示相關資訊
語句也可以:
string sql = string.format("insert into rulesetinfo(guid,rsname,rstype,rsadmin,rspath,rsdate,rsdesc,rsnote) values(,'','','','','','','')", guid, rulesetname, filetype, admin, rspath,datenow, rsdesc, note);
類的寫法
using system;using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.data;
using system.data.oledb;
/// /// 建立連線(開啟資料庫檔案)
///
public void open()
catch (exception)
}/// /// 斷開連線(關閉據庫檔案)
///
public void close()
/// /// 增
///
/// sql命令
/// 以datatable形式返回資料
public int getdatains(string sql)
catch (exception)
return row;
}/// /// 刪
///
///
///
public int getdatadel(string sql)
catch (exception)
return row;
}/// /// 改
///
///
///
public int getdataupd(string sql)
catch (exception)
return row;
}/// /// 查
///
///
///
public dataset getdatasel(string sql)
catch (exception)
return ds;
} }
}
C 操作Access資料庫
using system using system.collections.generic using system.linq using system.text using system.data using system.data.oledb using system.configuration...
c 操作ACCESS資料庫
c 簡單操作access資料庫 這兩天做專案,需要將資料存到資料庫,並進行一些簡單的增刪改操作,剛好記錄下access資料庫的基本操作方法,下文中提及到知識應用如有不正確的地方,大家多多指正,在程式開始時,我打算使用一些資料庫操作語句來建立乙個資料庫,不過好像用的不是很成功。而且如果要手動建立資料庫...
c 操作Access資料庫
先新增兩個com引用 第乙個 microsoft activex data objects 2.8 library 第二個 microsoft ado ext.2.8 for ddl and security 1.建立access資料庫 using adox adox.catalog cat new...