日期:2014-05-19  浏览次数:20595 次

初学者问一个查询问题!
页面上有一个Label1,数据库是Access。
那么在Page_Load事件中怎么将Table1表中的Field1字段的值赋给Label1显示?


------解决方案--------------------
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
// The insertSQL string contains a SQL statement that
// inserts a new row in the source table.
OleDbCommand command = new OleDbCommand( "select top 1 field1 from table1 ");

// Set the Connection to the new OleDbConnection.
command.Connection = connection;

// Open the connection and execute the insert command.
try
{
connection.Open();
Label1.Text = command.ExecuteScalar();
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
// The connection is automatically closed when the
// code exits the using block.
}

------解决方案--------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;//Access数据库要引入OLEDB命名空间

public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

OleDbConnection conn = new OleDbConnection( "数据库连接字符串 ");//打开数据库连接
string sql = "select * from Table1 where=条件 ";//SQL查询语句
OleDbCommand comm = new OleDbCommand(sql,conn);//执行SQL查询
conn.Open();//打开数据库
OleDbDataReader dr = comm.ExecuteReader();//执行命令,返回DataReader类的一个对象
if(dr.Read())//当有数据读取时
{
Label1.Text=dr[ "Field1 "].ToString();//给Label1.Text控件赋值
}
}
分给我吧。。。
我写这么详细