.net读取Sql数据
using System;
using System.Data;
using System.Configuration;
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.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string stringstrConnection = "server=Cityhr-z;uid=sa;pwd=;database=CityHRCorp";
//定义链接字符串stringstrConnection链接数据库
SqlConnection objConnection = new SqlConnection(stringstrConnection);
//SqlConnection来创建objConnection为新建一个链接内容是stringstrConnection
objConnection.Open();
//打开 objConnection链接
SqlCommand objCommand = new SqlCommand("select * from EMPLOYEES where employeeID=2", objConnection);
//用 SqlCommand 来定义objCommand 的内容为打开数据库的条件*("select * from EMPLOYEES where employeeID=2", objConnection);最后面加一个 objConnection来链接数据库
SqlDataReader objDataReader = objCommand.ExecuteReader();
//用SqlDataReader 来定义objDataReader,用ExecuteReader()来执行objCommand实现读取
if (objDataReader.Read())
//如果读取到数据就显示出来
{
id.Text = Convert.ToString(objDataReader["employeeID"]);
name.Text = Convert.ToString(objDataReader["employeename"]);
ip.Text = Convert.ToString(objDataReader["IPAddress"]);
}
objDataReader.Close();
//关闭链接
objConnection.Close();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
读取所以数据显示在页面上用while来实现.
using System;
using System.Data;
using System.Configuration;
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.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string stringstrConnection = "server=Cityhr-z;uid=sa;pwd=;database=CityHRCorp";
//定义链接字符串stringstrConnection链接数据库
SqlConnection objConnection = new SqlConnection(stringstrConnection);
//SqlConnection来创建objConnection为新建一个链接内容是stringstrConnection
objConnection.Open();
//打开 objConnection链接
// SqlCommand objCommand = new SqlCommand("select * from EMPLOYEES where employeeID=2", objConnection);
//用 SqlCommand 来定义objCommand 的内容为打开数据库的条件*("select * from EMPLOYEES where employeeID=2", objConnection);最后面加一个 objConnection来链接数据库
SqlCommand objCommand = new SqlCommand("select * from EMPLOYEES", objConnection);
SqlDataReader objDataReader = objCommand.ExecuteReader();
//用SqlDataReader 来定义objDataReader,用ExecuteReader()来执行objCommand实现读取
// if (objDataReader.Read())
//如果读取到数据就显示出来
//string outhrml = string.Empty;
while (objDataReader.Read())
{
//id.Text += (Convert.ToString(objDataReader["employeeID"]) + " , ");
//name.Text += (Convert.ToString(objDataReader["employeename"]) + " , ");
//ip.Text += (Convert.ToString(objDataReader["IPAddress"]) +" ,");
string outhrml = string.Empty;
outhrml += ("ID:" + Convert.ToString(objDataReader["employeeID"]));
outhrml += ("NAME:" + Convert.ToString(objDataReader["employeename"]));
outhrml += ("IPAddress:" + Convert.ToString(objDataReader["IPAddress"]));
outhrml += "<br />";
//Response.Write(outhrml);
id.Text += outhrml;
}
objDataReader.Close();
//关闭链接
objConnection.Close();
}
}
引自 http://blog.96yy.com/trackback.asp?tbID=15文
using System.Data;
using System.Configuration;
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.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string stringstrConnection = "server=Cityhr-z;uid=sa;pwd=;database=CityHRCorp";
//定义链接字符串stringstrConnection链接数据库
SqlConnection objConnection = new SqlConnection(stringstrConnection);
//SqlConnection来创建objConnection为新建一个链接内容是stringstrConnection
objConnection.Open();
//打开 objConnection链接
SqlCommand objCommand = new SqlCommand("select * from EMPLOYEES where employeeID=2", objConnection);
//用 SqlCommand 来定义objCommand 的内容为打开数据库的条件*("select * from EMPLOYEES where employeeID=2", objConnection);最后面加一个 objConnection来链接数据库
SqlDataReader objDataReader = objCommand.ExecuteReader();
//用SqlDataReader 来定义objDataReader,用ExecuteReader()来执行objCommand实现读取
if (objDataReader.Read())
//如果读取到数据就显示出来
{
id.Text = Convert.ToString(objDataReader["employeeID"]);
name.Text = Convert.ToString(objDataReader["employeename"]);
ip.Text = Convert.ToString(objDataReader["IPAddress"]);
}
objDataReader.Close();
//关闭链接
objConnection.Close();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
读取所以数据显示在页面上用while来实现.
using System;
using System.Data;
using System.Configuration;
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.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string stringstrConnection = "server=Cityhr-z;uid=sa;pwd=;database=CityHRCorp";
//定义链接字符串stringstrConnection链接数据库
SqlConnection objConnection = new SqlConnection(stringstrConnection);
//SqlConnection来创建objConnection为新建一个链接内容是stringstrConnection
objConnection.Open();
//打开 objConnection链接
// SqlCommand objCommand = new SqlCommand("select * from EMPLOYEES where employeeID=2", objConnection);
//用 SqlCommand 来定义objCommand 的内容为打开数据库的条件*("select * from EMPLOYEES where employeeID=2", objConnection);最后面加一个 objConnection来链接数据库
SqlCommand objCommand = new SqlCommand("select * from EMPLOYEES", objConnection);
SqlDataReader objDataReader = objCommand.ExecuteReader();
//用SqlDataReader 来定义objDataReader,用ExecuteReader()来执行objCommand实现读取
// if (objDataReader.Read())
//如果读取到数据就显示出来
//string outhrml = string.Empty;
while (objDataReader.Read())
{
//id.Text += (Convert.ToString(objDataReader["employeeID"]) + " , ");
//name.Text += (Convert.ToString(objDataReader["employeename"]) + " , ");
//ip.Text += (Convert.ToString(objDataReader["IPAddress"]) +" ,");
string outhrml = string.Empty;
outhrml += ("ID:" + Convert.ToString(objDataReader["employeeID"]));
outhrml += ("NAME:" + Convert.ToString(objDataReader["employeename"]));
outhrml += ("IPAddress:" + Convert.ToString(objDataReader["IPAddress"]));
outhrml += "<br />";
//Response.Write(outhrml);
id.Text += outhrml;
}
objDataReader.Close();
//关闭链接
objConnection.Close();
}
}
引自 http://blog.96yy.com/trackback.asp?tbID=15文