举例基于 Connection 的数据库连接程序Word下载.docx

上传人:b****4 文档编号:8164694 上传时间:2023-05-10 格式:DOCX 页数:9 大小:137.84KB
下载 相关 举报
举例基于 Connection 的数据库连接程序Word下载.docx_第1页
第1页 / 共9页
举例基于 Connection 的数据库连接程序Word下载.docx_第2页
第2页 / 共9页
举例基于 Connection 的数据库连接程序Word下载.docx_第3页
第3页 / 共9页
举例基于 Connection 的数据库连接程序Word下载.docx_第4页
第4页 / 共9页
举例基于 Connection 的数据库连接程序Word下载.docx_第5页
第5页 / 共9页
举例基于 Connection 的数据库连接程序Word下载.docx_第6页
第6页 / 共9页
举例基于 Connection 的数据库连接程序Word下载.docx_第7页
第7页 / 共9页
举例基于 Connection 的数据库连接程序Word下载.docx_第8页
第8页 / 共9页
举例基于 Connection 的数据库连接程序Word下载.docx_第9页
第9页 / 共9页
亲,该文档总共9页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

举例基于 Connection 的数据库连接程序Word下载.docx

《举例基于 Connection 的数据库连接程序Word下载.docx》由会员分享,可在线阅读,更多相关《举例基于 Connection 的数据库连接程序Word下载.docx(9页珍藏版)》请在冰点文库上搜索。

举例基于 Connection 的数据库连接程序Word下载.docx

,而是"

System.Data.SqlClient"

其次就是他的连接字符串了,我们一个一个参数来介绍(注意:

参数间用分号分隔):

id=sa"

:

连接数据库的验证用户名为sa.他还有一个别名"

uid"

所以这句我们还可以写成"

uid=sa"

password="

连接数据库的验证密码为空.他的别名为"

pwd"

所以我们可以写为"

pwd="

这里注意,你的SQL 

Server必须已经设置了需要用户名和密码来登录,否则不能用这样的方式来登录.如果你的SQL 

Server设置为Windows登录,那么在这里就不需要使用"

id"

和"

password"

这样的方式来登录,而需要使用"

Trusted_Connection=SSPI"

来进行登录. 

catalog=Northwind"

使用的数据源为"

Northwind"

这个数据库.他的别名为"

Database"

本句可以写成"

Database=Northwind"

Server=YourSQLServer"

使用名为"

YourSQLServer"

的服务器.他的别名为"

Data 

Source"

"

Address"

Addr"

.如果使用的是本地数据库且定义了实例名,则可以写为"

Server=(local)\实例名"

如果是远程服务器,则将"

(local)"

替换为远程服务器的名称或IP地址. 

连接超时时间为30秒. 

在这里,建立连接对象用的构造函数为:

SqlConnection. 

一、基于Connection的数据库连接程序

编写一个Web应用程序,要求实现Connection对象的创建,并连接SQLServer的Northwind数据库。

操作步骤

1.建立ASP.NET网站

在VisualStudio.NET集成开发环境中,新建一个名为Example的ASP.NET网站,选择在文件系统E:

\asp8中建立,选择C#语言。

2.添加窗体页面

在“解决方案管理器”窗口,右击项目名称“Example”,在弹出的快捷菜单中选择“添加新项”菜单命令,添加一个命名为“ConnectionExample.aspx”的窗体,单击“添加”按钮。

3.加载命名空间

因为需要连接SQLServer数据库,因此在ConnectionExample.aspx.cs文件的开头加载命名空间。

usingSystem.Data.SqlClient;

4.创建一个SqlConnection对象

在ConnectionExample.aspx.cs文件的Page_Load事件里添加如下代码。

stringstrConnection="

Server=(local);

Database=Northwind;

UserID=sa;

Password=;

ConnectionTimeout=30"

SqlConnectionobjConnection=newSqlConnection(strConnection);

objConnection.Close();

5.保存、编译程序

二、基于Command的简单数据读取程序

编写一个Web应用程序,在Connection对象的基础之上,创建Command对象,并读取数据源中的数据。

分析:

为加深对Command对象的理解,在实例中使用构造函数创建Command对象。

由于数据提供者为SQLServer的Northwind数据库,因此使用类SqlCommand的构造函数。

要读取数据源中的数据,可利用Command对象的ExecuteReader方法创建DataReader对象,利用Response对象的Write方法显示DataReader结果集中的数据。

新建一个名为“Example”的ASP.NET网站,选择在文件系统E:

\asp8中建立。

在“解决方案管理器”窗口,添加一个命名为“CommandExample.aspx”的窗体。

因为需要连接SQLServer数据库,因此,在“CommandExample.aspx.cs”文件的开头加载命名空间。

4.创建一个SqlCommand对象

在“CommandExample.aspx.cs”文件的Page_Load事件里添加如下代码。

protectedvoidPage_Load(objectsender,EventArgse){

stringstrConnection="

User

ID=sa;

SqlConnectionobjConnection=newSqlConnection(strConnection);

try{objConnection.Open();

SqlCommandcmd=newSqlCommand("

select*fromregion"

objConnection);

SqlDataReaderreader=cmd.ExecuteReader();

for(inti=0;

i<

reader.FieldCount;

i++)

Response.Write(reader.GetName(i)+"

&

nbsp;

);

Response.Write("

<

br>

Response.Write("

hrwidth=20%

align=left>

while(reader.Read()){

Response.Write(reader.GetValue(i)+"

"

}}

catch(SqlExceptionex){Response.Write(ex.Message.ToString());

}

finally{objConnection.Close();

}}

5.保存、编译、运行程序

在“解决方案资源管理器”窗口中的“CommandExample.aspx”文件上右击,在快捷菜单中选择“设为起始页”菜单命令,然后单击工具栏上的“启动”按钮,运行该程序。

三、基于DataReader的简单数据访问程序

编写一个Web应用程序,使用Connection、Command、DataReader这3个对象实现数据访问。

分析

DataReader对象可以方便快捷地读取数据,在一些特殊场合,如应用程序不需要对数据做任何修改,只是读取和显示,使用DataReader对象可以大大节省系统资源。

在VisualStudio.NET集成开发环境中,新建一个名为“Example”的ASP.NET网站,选择在文件系统E:

在“解决方案管理器”窗口中右击项目名称“Example”,在弹出的快捷菜单中选择“添加新项”菜单命令,添加一个命名为“DataReaderExample.aspx”的窗体,单击“添加”按钮。

3.添加控件

在“DataReaderExample.aspx”的“设计”页面上放置3个GridView控件。

4.修改Web.config

在Web.config文件的<

configuration>

与<

/configuration>

之间添加如下用于数据库连接的代码。

appSettings>

addkey="

value="

database=Northwind;

server=localhost;

uid=sa;

pwd=;

>

<

/add>

/appSettings>

5.加载命名空间

因为需要连接SQLServer数据库,因此,在“DataReaderExample.aspx.cs”文件的开头加载命名空间。

6.创建一个SqlDataReader对象

在“DataReaderExample.aspx.cs”文件的Page_Load事件里添加如下代码。

protectedvoidPage_Load(objectsender,System.EventArgse){

StringstrConnection=System.Configuration.ConfigurationSettings.AppSettings["

];

SqlConnectionconn=newSqlConnection(strConnection);

StringstrCount="

SelectCount(ProductName)asProductNumFrom[Products]"

conn.Open();

SqlCommandcmd=newSqlCommand(strCount,conn);

SqlDataReaderreader=cmd.ExecuteReader();

GridView1.DataSource=reader;

GridView1.DataBind();

reader.Close();

StringstrSql="

Selecttop3ProductName,CompanyName,UnitPrice"

+"

From[Products],[Suppliers]"

+"

Where[Products].SupplierID=[Suppliers].SupplierIDOrderby

ProductId;

Selecttop3CompanyName,ContactName,Address"

From[Customers]OrderbyCustomerId"

cmd.CommandText=strSql;

reader=cmd.ExecuteReader();

GridView2.DataSource=reader;

GridView2.DataBind();

reader.NextResult();

GridView3.DataSource=reader;

GridView3.DataBind();

conn.Close();

7.保存、编译、运行程序

在“解决方案资源管理器”窗口中的“DataReaderExample.aspx”文件上右击,在快捷菜单中选择“设为起始页”菜单命令,然后单击工具栏上的“启动”按钮,运行该程序。

注意:

Web.config文件的使用

本例在Web.config中创建了一个名为“Northwind”的key,通过引用key字段的值获得连接字符串。

当包含连接字符串的文件很多时,如需更改字符串,只需要更改Web.config的连接字符串,这样,所有引用该key值的文件都不需要单独修改。

四、基于ADO.NET的人员基本信息管理系统

编写一个Web应用程序,实现简单的人员基本信息管理,具有基本的增删查改功能。

该程序要求实现人员基本信息基本的增删查改功能,因此首先可以使用GridView控件实现编辑和删除功能,其次可以单独做添加和查询小模块;

根据程序的功能要求以及考虑到用户操作的方便性,应在一个网页上实现图书信息的列表显示、添加、查询、编辑等功能。

1.建立网站

在VisualStudio.NET集成开发环境中新建一个名为“Example”的ASP.NET网站,选择在文件系统E:

2.添加页面控件

选择“Default.aspx”设计视图,在页面中添加下表所示控件。

设计页面如下图所示。

3.定义全局变量

stringStrConnection="

server=local;

database=test;

SqlConnectionconn;

SqlCommandcmd;

4.编辑Page_Load方法

protectedvoidPage_Load(objectsender,EventArgse){

if(!

IsPostBack){binddata();

5.编辑自定义binddata方法

protectedvoidbinddata(){

conn=newSqlConnection(StrConnection);

conn.Open();

Stringsql="

select*fromPeople"

SqlDataAdaptermyda=newSqlDataAdapter(sql,conn);

DataSetds=newDataSet();

myda.Fill(ds,"

People"

GridView1.DataSource=ds.Tables["

].DefaultView;

6.编辑GridView1的“删除”按钮处理方法

01:

protectedvoidGridView1_RowDeleting(objectsender,GridViewDeleteEventArgse){

02:

03:

Stringid=this.GridView1.DataKeys[e.RowIndex].Value.ToString();

04:

intidt=int.Parse(id);

05:

cmd=newSqlCommand();

cmd.Connection=conn;

06:

cmd.CommandText="

deletefromPeoplewherepkID="

+idt;

07:

cmd.ExecuteNonQuery();

binddata();

7.编辑GridView1的“编辑”按钮处理方法

protectedvoidGridView1_RowEditing(objectsender,GridViewEditEventArgse){

GridView1.EditIndex=e.NewEditIndex;

8.编辑GridView1的“更新”按钮处理方法

protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEvent

Argse){

stringid=GridView1.DataKeys[e.RowIndex].Values[0].ToString();

stringpkid=GridView1.Rows[e.RowIndex].Cells[2].Text.ToString();

stringname=((TextBox)GridView1.Rows[e.RowIndex].Cells[3].

Controls[0]).Text.Trim();

stringsex=((TextBox)GridView1.Rows[e.RowIndex].Cells[4].

Controls[0]).Text.Trim();

stringaddress=((TextBox)GridView1.Rows[e.RowIndex].Cells[5].

stringborn=((TextBox)GridView1.Rows[e.RowIndex].Cells[6].

08:

stringSqlStr="

updatePeoplesetpkID='

+pkid+"

'

Name='

+name+

Sex='

+sex+"

Address='

+address+"

Born='

+born+"

wherepkID="

+pkid;

09:

10:

cmd=newSqlCommand(SqlStr,conn);

11:

cmd.Dispose();

12:

GridView1.EditIndex=-1;

9.编辑GridView1的“取消”按钮处理方法

protectedvoidGridView1_RowCancelingEdit(objectsender,GridViewCancel

EditEventArgse)

{GridView1.EditIndex=-1;

10.编辑“添加”按钮处理方法和自定义init方法

protectedvoidButton2_Click(objectsender,EventArgse){

SqlCommandcmd=newSqlCommand("

insertintoPeople(pkID,Name,Sex,

Address,Born)values(@pkID,@Name,@Sex,@Address,@Born)"

conn);

cmd.Parameters.Add("

@pkID"

SqlDbType.VarChar);

cmd.Parameters.Add

("

@Name"

@Sex"

@Address"

@Born"

cmd.Parameters

["

].Value=TBpkID.Text;

cmd.Parameters["

].Value=TBName.Text;

].Value=RBsex.SelectedItem.Text;

].Value=TBAddress.Text;

].Value=TBBorn.Text;

init();

}

protectedvoidinit(){

13:

TBpkID.Text="

TBpkID.Focus();

TBName.Text="

14:

RBsex.SelectedIndex=-1;

TBAddress.Text="

TBBorn.Text="

11.编辑“查询”按钮处理方法

protectedvoidButton3_Click(objectsender,EventArgse){

Stringid=TBpkID1.Text;

select*fromPeoplewhere

pkID="

+id;

SqlDataAdaptermyda=newSqlDataAdapter(sql,conn);

DataSetds=newDataSet();

GridView2.DataSource=ds.Tables["

12.保存、编译、执行

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2