怎样在数据库中读取一项数据?

benniao2 2004-07-30 12:17:46
我想把下面查询出来的数据付给一个变量
select [biao].id from [biao] where [biao].name='abc'
怎样才能实现呢?用访问数据库的控件是不是太麻烦
...全文
74 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
aliketen 2004-07-30
  • 打赏
  • 举报
回复
DataGrid绑定结果
benniao2 2004-07-30
  • 打赏
  • 举报
回复
谢谢
lxcc 2004-07-30
  • 打赏
  • 举报
回复
<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

void Page_Load(object sender, EventArgs e) {
if(!IsPostBack)
{
BindGrid();
}
}

void Button1_Click(object sender, EventArgs e) {
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 as [Last Name], au_fname as [First Name], Address, City, State from Authors";

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

myConnection.Open();

DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
DataGrid1.DataBind();
}

</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<h2>Simple Data Report
</h2>
<hr size="1" />
<form runat="server">
<asp:datagrid id="DataGrid1" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black" EnableViewState="False">
<HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
</asp:datagrid>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
</form>
</body>
</html>
zpisgod 2004-07-30
  • 打赏
  • 举报
回复
不用控件,但是需要调用ado.net组件
以sqlserver数据库为例:
SqlConnection sconn=new SqlConnection("数据库链接字符串");//建立连接
SqlCommand scomm=new SqlCommand();
scomm.Connection=sconn;
scomm.CommandText="select [biao].id from [biao] where [biao].name='abc'";
//声明command对象并初始化
sconn.Open();//打开连接
SqlDataReader sdr=scomm.ExecuteReader();//声明datareader对象,并执行command对其填充
if(sdr.Read())
string id=sdr["id"].ToString(); //如果有数据则赋值给变量id
sdr.Close(); //关闭reader
sconn.Close();//关闭connection
ycc2008 2004-07-30
  • 打赏
  • 举报
回复
OleDbDataReader dr=......;
string aa=dr["bb"].ToString();
fly_miss 2004-07-30
  • 打赏
  • 举报
回复
不用访问数据库的控件的控件,这么读出数据

62,040

社区成员

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

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

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

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