这篇文章只适合初学者,如果你是大虾,这篇文章就是浪费你的时间。
首先,文件->兴建->项目。项目类型: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");
我的文章就此为止。
转载国内最大的酷站演示中心!