■■第一次做数据库连接,不知如何实现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#。。。谢谢大家!!!帮个忙,做个好心!! :)
...全文
321 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
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]都不行呢。到底出了什么错?

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧