手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >在ASP.NET中连接SQL Server的简单方法
在ASP.NET中连接SQL Server的简单方法
摘要:首先导入名字空间:System.Data和System.Data.SqlClient.详细代码看源程序.复制代码代码如下:protected...

首先导入名字空间:System.Data和System.Data.SqlClient.详细代码看源程序.

复制代码 代码如下:

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<script language="C#" runat="server">

protected void Page_Load(Object Src, EventArgs E )

{

SqlConnection myConn = new SqlConnection("server=localhost;uid=sa;pwd=;database=pubs");

//创建对象SqlConnection

string strSQL="SELECT au_id,au_lname,au_fname,phone,address,city,zip FROM authors";

SqlDataAdapter myCmd = new SqlDataAdapter(strSQL, myConn);

//创建对象SqlDataAdapter

DataSet ds = new DataSet();

//创建对象DataSet

myCmd.Fill(ds);

//填充数据到Dataset

DataView source = new DataView(ds.Tables[0]);

MyDataGrid.DataSource = source ;

MyDataGrid.DataBind();

//将数据绑定到DataGrid

}

</script>

<body>

<h3><font face="Verdana">Simple SELECT to a DataGrid Control

</font></h3>

<ASP:DataGrid id="MyDataGrid" runat="server"

Width="600"

BackColor="#ccccff"

BorderColor="black"

ShowFooter="false"

CellPadding=3

CellSpacing="0"

Font-Name="Verdana"

Font-Size="8pt"

HeaderStyle-BackColor="#aaaadd"

MaintainState="false"

/>

</body>

</html>

【在ASP.NET中连接SQL Server的简单方法】相关文章:

asp.net导出excel的简单方法

使用.NET存储XML数据的方法

ASP.NET防止页面刷新的两种解决方法

ASP.NET技巧:access下的分页方案

用ASP.NET实现简单的文字水印

ASP.NET中 Execl导出的六种方法实例

ASP.NET中文件上传下载方法集合

ASP.NET列出数据库活跃链接的方法

ASP.NET中ServerPush用法实例分析

ASP.NET中为GridView添加删除提示框的方法

精品推荐
分类导航