在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] 知了!
似乎创建一个连接类更好一点,因为需要经常用到连接的!
好东西拿下
好东西拿下页:
[1]
