中国教程网论坛's Archiver

寅生 发表于 2006-5-1 08:17

在ASP.NET中的数据库连接

在ASP.NET中,如何连接数据库?

    我们用C#来实现,见下列代码:


[code]< %@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
< script language="C#" runat="server">
' 声明C#
public DataSet dsCustomer;
protected void Page_Load(Object Src, EventArgs E )
{
' 在打开页面时连接数据库
SQLConnection myConnection = new SQLConnection
("server=sql2.bitsonthewire.com;uid=joeuser;pwd=joeuser;database=northwind");
SQLDataSetCommand CustomersDSCommand = new SQLDataSetCommand("select * from customers", myConnection);
dsCustomer = new DataSet();
CustomersDSCommand.FillDataSet(dsCustomer,"Customers");
foreach (DataRow Customer in dsCustomer.Tables["Customers"].Rows)
{
Response.Write(Customer["CustomerId"].ToString() + "<br> " ); } }
</script>[/code]

redhaste 发表于 2006-5-27 17:28

知了!

似乎创建一个连接类更好一点,因为需要经常用到连接的!

qbq1234 发表于 2008-7-30 16:07

好东西拿下

好东西拿下

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.