■■第一次做数据库连接,不知如何实现ASP的rs("XX")■■

minghui000 2004-05-03 04:07:27
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.OleDb"%>
<%@Page Language="C#" debug=true runat=server%>
<script runat=server>
void Page_Load(Object sender,EventArgs e){
}
</script>

<%
OleDbConnection Conn=new OleDbConnection();
//得到web.config中数据库的完整路径
Conn.ConnectionString=ConfigurationSettings.AppSettings["databaseconn"] + Server.MapPath(ConfigurationSettings.AppSettings["databasepath"]);
//Conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;"+"Data Source="+Server.MapPath("db1.mdb");
Conn.Open();
OleDbCommand Comm=new OleDbCommand("Select * from ebooks",Conn);
OleDbDataReader rs=Comm.ExecuteReader();
%>

<table border="1" width="72%" cellspacing="0" cellpadding="0" bordercolor="#008000">
<tr>
<td>ID</td>
<td>姓名</td>
<td>标题</td>
<td>时间</td>
<td>删除</td>
</tr>
<tr>
<td> <%Response.Write(rs["user"]);%></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>


//要求用C#。。。谢谢大家!!!帮个忙,做个好心!! :)
...全文
205 16 点赞 打赏 收藏 举报
写回复
16 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wanjinbao 2004-05-06
在给你一些经典的链接方式:
SQL Server
ODBC


Standard Security:
"Driver={SQL Server};Server=Aron1;Database=pubs;Uid=sa;Pwd=asdasd;"


Trusted connection:
"Driver={SQL Server};Server=Aron1;Database=pubs;Trusted_Connection=yes;"


Prompt for username and password:
oConn.Properties("Prompt") = adPromptAlways
oConn.Open "Driver={SQL Server};Server=Aron1;DataBase=pubs;"


OLEDB, OleDbConnection (.NET)


Standard Security:
"Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"


Trusted Connection:
"Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI;"
(use serverName\instanceName as Data Source to use an specifik SQLServer instance, only SQLServer2000)
Prompt for username and password:
oConn.Provider = "sqloledb"
oConn.Properties("Prompt") = adPromptAlways
oConn.Open "Data Source=Aron1;Initial Catalog=pubs;"


Connect via an IP address:
"Provider=sqloledb;Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;"
(DBMSSOCN=TCP/IP instead of Named Pipes, at the end of the Data Source is the port to use (1433 is the default))
SqlConnection (.NET)


Standard Security:
"Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"


Trusted Connection:
"Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI;"
(use serverName\instanceName as Data Source to use an specifik SQLServer instance, only SQLServer2000)
Connect via an IP address:
"Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;"
(DBMSSOCN=TCP/IP instead of Named Pipes, at the end of the Data Source is the port to use (1433 is the default))
Declare the SqlConnection:

C#:
using System.Data.SqlClient;
SqlConnection oSQLConn = new SqlConnection();
oSQLConn.ConnectionString="my connectionstring";
oSQLConn.Open();

VB.NET:
Imports System.Data.SqlClient
Dim oSQLConn As SqlConnection = New SqlConnection()
oSQLConn.ConnectionString="my connectionstring"
oSQLConn.Open()


Data Shape


MS Data Shape
"Provider=MSDataShape;Data Provider=SQLOLEDB;Data Source=Aron1;Initial Catalog=pubs;User ID=sa;Password=asdasd;"
  • 打赏
  • 举报
回复
minghui000 2004-05-06
楼上答得好。。分规你!!我就喜欢你这样负责任的人
  • 打赏
  • 举报
回复
nowfox 2004-05-05
Conn.Open();
OleDbCommand Comm=new OleDbCommand("Select * from ebooks",Conn);
OleDbDataReader rs=Comm.ExecuteReader();
while(rs.Read())
{
……
……
Response.Write(rs["UserName"].ToString());
……
……
}
  • 打赏
  • 举报
回复
minghui000 2004-05-05
有时候新东西不一定比旧东西好。。我喜欢用 rs[]
  • 打赏
  • 举报
回复
DB牛牛 2004-05-05
在rs.item[1]的后面加上.value
rs.item[1].value 写成这样就可以了
  • 打赏
  • 举报
回复
lxcc 2004-05-05
干吗不用ADO.NET和datagrid

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

void Page_Load(object sender, EventArgs e) {

if (!Page.IsPostBack) {

// Databind the data grid on the first request only
// (on postback, rebind only in paging command)

BindGrid();
}
}

void DataGrid_Page(object sender, DataGridPageChangedEventArgs e) {

DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}

void BindGrid() {

// TODO: update the ConnectionString and CommandText values for your application
string ConnectionString = "server=(local);database=pubs;trusted_connection=true";
string CommandText = "select au_lname, au_fname, address, city, state from Authors order by au_lname";

SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter myCommand = new SqlDataAdapter(CommandText, myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds);

DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}

</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<h2>Data Report with Paging
</h2>
<hr size="1" />
<form runat="server">
<asp:datagrid id="DataGrid1" runat="server" AllowPaging="true" PageSize="6" OnPageIndexChanged="DataGrid_Page" ForeColor="Black" BackColor="White" CellPadding="3" GridLines="None" CellSpacing="1" width="80%">
<HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
<PagerStyle horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages"></PagerStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
</asp:datagrid>
</form>
</body>
</html>
  • 打赏
  • 举报
回复
njxiaod 2004-05-05
构建Web解决方案——应用ASP.NET和ADO.NET

给你源代码学习学习吧

我觉得不错 都可以调试

给你开个 http://218.94.22.24/xiaod/xiaod.rar
  • 打赏
  • 举报
回复
minghui000 2004-05-05
试试看。。。有没更详细的例子呢?
  • 打赏
  • 举报
回复
minghui000 2004-05-04
up
  • 打赏
  • 举报
回复
minghui000 2004-05-03
有没更详细的例子。。
  • 打赏
  • 举报
回复
skyover 2004-05-03
DataRow["xx"]
  • 打赏
  • 举报
回复
smx717616 2004-05-03
你说的rs("字段名") 在 .net中是没的 ,不过你可以 用
OleDbDataReader rs
rs.item(i) i 代表列,从0算起
  • 打赏
  • 举报
回复
riconyi 2004-05-03
记住一点,你现在是用.net,这里面用的是ado.net,所以不要将你asp下的思想带到这里来,建议买本书看看
  • 打赏
  • 举报
回复
itfly 2004-05-03
while(Rs.read())
{
Rs["user"].ToString();
}
  • 打赏
  • 举报
回复
guanvee 2004-05-03
建议不要老往asp那边靠,觉得差距实在太大了,完全不同的两种东西,我以前就是用asp的,而且没有windows编程的经验,转向asp.net觉得很不适应,慢慢来吧,多熟悉.net的东东吧,代码界面分开,最好用vs.net集成环境开发。
想起来还是asp简单,不过代码太多太乱了。
努力学习中。。。
  • 打赏
  • 举报
回复
minghui000 2004-05-03
我用rs.item[1]都不行呢。到底出了什么错?
  • 打赏
  • 举报
回复
相关推荐
发帖
.NET社区
加入

5.9w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2004-05-03 04:07
社区公告
暂无公告