我对asp.net的认识.txt
来自「.net常用经典源代码。十分好用!不用客气」· 文本 代码 · 共 98 行
TXT
98 行
对于数据库的连接这块:
asp.net中的数据库连接字符串省去了驱动提供者,而这个提供者变成了在代码隐藏文件中引入的一个包:using System.Data.SqlClient;
String connectionString = "server=localhost; uid=sa; pwd=; database=northwind";
SQLConnection myConn = new SQLConnection(connectionString);
myConn.Open();
...
myConn.Close();
----------------------------------------------------------------
Connections--连接和管理数据库事务。
Commands--向数据库发送的命令。
DataReaders--直接读取流数据。
DateSets 和 DateSetCommands--对驻留内存中的数据进行存储和操作。
-----------------------------------------------------------------
SQLDataReader
[C#]
SQLDataReader myReader = null;
myCommand.Execute(out myReader);
------------------------------------------------------------------
ADOCommand
[C#]
String SQLStmt = " SELECT * FROM Customers";
ADOCommand myCommand = new ADOCommand(SQLStmt, myConn);
------------------------------------------------------------------
ADODataReader
[C#]
ADODataReader myReader = null;
myCommand.Execute(out myReader);
------------------------------------------------------------------
接下来这步是一个使用DataReader的简单格式
[C#]
While (myReader.Read()) {
[C#]
While (myReader.Read()) {
// do your thing with the current row here
}
------------------------------------------------------------------
下面的例子展示了迄今为止我们所讨论的内容:建立一个到SQL数据源的连接,对于连接的发送select命令,用DataReader对象来保存返回的结果,然后通过循环DataReader取得数据。
下面是用C#写的完整代码。
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<html>
<head>
<script language="C#" runat="server">
public SQLDataReader myReader;
public String html;
protected void Page_Load(Object Src, EventArgs E ) {
SQLConnection mySQLConnection = new SQLConnection("server=localhost;uid=sa;pwd=;database=northwind");
SQLCommand mySQLCommand = new SQLCommand("select * from customers", mySQLConnection);
try {
mySQLConnection.Open();
mySQLCommd.Execute(out myReader);
html="<Table>";
html+="<TR>";
html+="<TD><B>Customer ID</B></TD>";
html+="<TD><B>Company Name</B></TD>";
html+="</TR>";
while (myReader.Read()) {
html+="<TR>";
html+="<TD + myReader["CustomerID"].ToString() + "</TD>";
html+="<TD>" + myReader["CompanyName"].ToString() + "</TD>";
html+="</TR>";
}
html+="</Table>";
}
catch(Exception e) {
html=e.ToString();
}
finally
{
meader.Close();
mySQLConnection.Close();
}
Response.Write(html);
}
</script>
</head>
<body>
</body>
</html>
-------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?