日期:2014-05-18  浏览次数:20439 次

有关ASP。NET从数据库表中读取表项值??急急急
我用ASP。NET设计一个物流配送系统
我建表时要从某个表中读取表中某几列的值该怎么实现?
有谁会的能教教我吗?
谢谢了!
eg:     user表中有UID,Uname,Upassword,Uaddress,Usex几个表项
我现在要通过语句来从user表中读取UID,Uname,Upassword应该怎么写?
SqlConnection   cn   =   new   SqlConnection(Confurgiration.AppSettings[ "connstr "]);
string   sql   =   "select   UID,Uname,Upassword   from   user   ";
SqlCommand   cm   =   new   SqlCommand(sql,cn);
cm.Connection.Open();
SqlDataReader   dr   =   cm.Excecuter();
后面的该怎么写
我怎么才能将UID,Uname,Upassword   都读出来?
请高手多多指教了!
谢谢了!


------解决方案--------------------


SqlConnection cn = new SqlConnection(Confurgiration.AppSettings[ "connstr "]);
string sql = "select UID,Uname,Upassword from user ";
SqlCommand cm = new SqlCommand(sql);
SqlDataAdapter sda=new SqlDataAdapter(cm,cn);
cm.Connection.Open();
DataSet ds;
sda.Fill(ds)


然后取得就可以了!

------解决方案--------------------
SqlConnection cn = new SqlConnection(Confurgiration.AppSettings[ "connstr "]);
string sql = "select UID,Uname,Upassword from [user] "; //user关键字,加中括号
SqlCommand cm = new SqlCommand(sql,cn);
cm.Connection.Open();
SqlDataReader dr = cm.ExcecuteReader();
while(dr.Read())
{
//在这里你可以赋值给其他变量
Response.Write(dr[ "UID "] + " " + dr[ "Uname "] + " " + dr[ "Upassword "]);
}

------解决方案--------------------
1楼的正解,把数据读到DS中就可以很方便的操作了。
------解决方案--------------------
SqlConnection cn = new SqlConnection(Confurgiration.AppSettings[ "connstr "]);
string sql = "select UID,Uname,Upassword from user ";
SqlCommand cm = new SqlCommand(sql);
SqlDataAdapter sda=new SqlDataAdapter(cm,cn);
cm.Connection.Open();
DataSet ds;
sda.Fill(ds)


这个方法ok