這篇文章只適合初學者,如果你是大蝦,這篇文章就是浪費你的時間。
首先,文件->興建->項目。項目類型:c#,asp。net應用程序。
在工具箱中拖一個sqldataadapter到webform窗口中。
向導先點下一步,新建連接,在連接標簽下,選取一個可用的服務器和可用的sqlserver數據庫,確定。
然后配置查詢生成器(太簡單就不說了)。完成。
在webform中點擊sqldataadapter1在屬性欄的下面點生成數據集。單選框用新建,確定。
至此,和數據庫的連接工作基本搞定了,讓我們來操作它吧。
拖一個datagrid到webform 窗口。屬性生成器中選好數據源(我們只有一個dataset)也就這樣了。
然后我們來看看代碼。
切換到代碼窗口,添加黃色的代碼:
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace webapplication1
{
/// <summary>
/// summary description for webform1.
/// </summary>
public class webform1 : system.web.ui.page
{
protected system.data.sqlclient.sqldataadapter sqldataadapter1;
protected system.data.sqlclient.sqlcommand sqlselectcommand1;
protected system.data.sqlclient.sqlcommand sqlinsertcommand1;
protected system.data.sqlclient.sqlconnection sqlconnection1;
protected system.web.ui.webcontrols.datagrid datagrid1;
protected system.web.ui.webcontrols.button button1;
protected system.data.sqlclient.sqldataadapter sqldataadapter2;
protected system.data.sqlclient.sqlcommand sqlselectcommand2;
protected system.data.sqlclient.sqlcommand sqlinsertcommand2;
protected system.data.sqlclient.sqlcommand sqlupdatecommand1;
protected system.data.sqlclient.sqlcommand sqldeletecommand1;
protected system.data.sqlclient.sqlconnection sqlconnection2;
protected system.web.ui.webcontrols.datagrid datagrid2;
protected webapplication1.dataset1 dataset11;
public webform1()
{
page.init += new system.eventhandler(page_init);
}
private void page_load(object sender, system.eventargs e)
{
// put user code to initialize the page here
}
private void page_init(object sender, eventargs e)
{
//
// codegen: this call is required by the asp.net web form designer.
//
initializecomponent();
//填充數據集
sqldataadapter1.fill (dataset11);
//綁定
datagrid1.databind ();
} #region web form designer generated code
/// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
#endregion
}
生成程序,如果正常的話你應該可以在datagrid看到你的數據了。
如果你對操作的數據有更高的要求的話(前提是會sql語句),你可以點饑前面綠色代碼前的小加號,修改下面的蘭色代碼。
private void initializecomponent()
{
this.sqlconnection1 = new system.data.sqlclient.sqlconnection();
this.dataset11 = new webapplication1.dataset1();
this.sqlinsertcommand1 = new system.data.sqlclient.sqlcommand();
this.sqlselectcommand1 = new system.data.sqlclient.sqlcommand();
this.sqldataadapter1 = new system.data.sqlclient.sqldataadapter();
this.sqldataadapter2 = new system.data.sqlclient.sqldataadapter();
this.sqlselectcommand2 = new system.data.sqlclient.sqlcommand();
this.sqlinsertcommand2 = new system.data.sqlclient.sqlcommand();
this.sqlupdatecommand1 = new system.data.sqlclient.sqlcommand();
this.sqldeletecommand1 = new system.data.sqlclient.sqlcommand();
this.sqlconnection2 = new system.data.sqlclient.sqlconnection();
((system.componentmodel.isupportinitialize)(this.dataset11)).begininit();
this.button1.click += new system.eventhandler(this.button1_click);
//
// sqlconnection1
//
this.sqlconnection1.connectionstring = "data source=(local);initial catalog=xr;integrated security=sspi;persist security " +
"info=false;workstation id=xurui;packet size=4096";
//
// dataset11
//
this.dataset11.datasetname = "dataset1";
this.dataset11.locale = new system.globalization.cultureinfo("zh-cn");
this.dataset11.namespace = "http://www.tempuri.org/dataset1.xsd";
//
// sqlinsertcommand1
//
this.sqlinsertcommand1.commandtext = "insert into store(zipcode, area, name) values (@zipcode, @area, @name); select zi" +
"pcode, area, name from store";
this.sqlinsertcommand1.connection = this.sqlconnection1;
this.sqlinsertcommand1.parameters.add(new system.data.sqlclient.sqlparameter("@zipcode", system.data.sqldbtype.char, 10, system.data.parameterdirection.input, true, ((system.byte)(0)), ((system.byte)(0)), "zipcode", system.data.datarowversion.current, null));
this.sqlinsertcommand1.parameters.add(new system.data.sqlclient.sqlparameter("@area", system.data.sqldbtype.char, 10, system.data.parameterdirection.input, true, ((system.byte)(0)), ((system.byte)(0)), "area", system.data.datarowversion.current, null));
this.sqlinsertcommand1.parameters.add(new system.data.sqlclient.sqlparameter("@name", system.data.sqldbtype.char, 10, system.data.parameterdirection.input, true, ((system.byte)(0)), ((system.byte)(0)), "name", system.data.datarowversion.current, null));
//
// sqlselectcommand1
//
this.sqlselectcommand1.commandtext = "select zipcode, area, name from store ";
this.sqlselectcommand1.connection = this.sqlconnection1;
//
// sqldataadapter1
//
this.sqldataadapter1.insertcommand = this.sqlinsertcommand1;
this.sqldataadapter1.selectcommand = this.sqlselectcommand1;
this.sqldataadapter1.tablemappings.addrange(new system.data.common.datatablemapping[] {
new system.data.common.datatablemapping("table", "store", new system.data.common.datacolumnmapping[] {....................................
...........................
你可以根據自己的需要生成數據集。
加入新列你可以這樣做:
datatable dt;//定義一個表(廢話)
dt=dataset11.tables ["你的表"];//給表付值
datarow dr;
dr=dt.newrow ();//新元組
dr.beginedit ();
dr["屬性1"]= ;
dr["屬性2"]=;
dr["屬性3"]=;
dr.endedit ();
dt.rows.add (dr);//加入元組
datagrid1.databind ();//把改變在datagrid中表示出來,不寫也可以但是沒現象了。
修改的代碼:
dr= tblauthors.rows.find("213-46-8915");//找到元組(缺省為找主碼)
dr.beginedit();
dr["屬性1"] = "342" ;
dr("屬性2")=“2342”;
dr.endedit()
刪除代碼:
drcurrent = tblauthors.rows.find("993-21-3427");
drcurrent.delete();
以上的操作只是對數據集 如果你要把改變保存回數據庫還要添一句:
sqldataadapter1.update (dataset11,"store");
我的文章就此為止。
轉載國內最大的酷站演示中心!