c#如何获得查询结果的某行某列值

yuanzhaofu01 2008-11-30 11:42:00
在c#中如何获得查询结果的某行某列值。
我举个例子:在ASP中
用下面的进行查询,
set rs=conn.execute("select Id,UserName,Password,sex,age,Tel,Email from user where id="&1)
这样就可以查出ID=1的相关信息
如果我要UserName,Password,sex,age,Tel,Email..可以这样表示
dim a,b,c,d,e,f,g
a=rs("UserName"):b=rs("Password"):c=rs("sex")...
现在这样一个问题用 c#怎么实现?
请教各位。
比如我有个三个textbox分别想绑定查出来的UserName,Age,sex该怎么写?
txtUserName.text=
txtAge.text=
txtsex.text=

如果查询结果有多行,那又该如何处理?
请教各位,希望能给个详细点的例子。
这先谢了
...全文
517 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
spidershark 2008-12-01
  • 打赏
  • 举报
回复
System.Data.SqlClient.SqlParameter[] p = new System.Data.SqlClient.SqlParameter[2];
p[0] = new System.Data.SqlClient.SqlParameter("@StartDate",SqlDbType.VarChar,20);
p[0].Value = StartDate;
p[1] = new System.Data.SqlClient.SqlParameter("@EndDate",SqlDbType.VarChar,20);
p[1].Value = EndDate;
DataTable dt = SqlHelper.SqlHelper.dtExecuteProce("DayOutputAnalysis",p);
DataView dv = dt.DefaultView ;
if(dv.Count > 0)
{
try
{
this.Label4.Text = dv.Table.Rows[0]["yearoutputs"].ToString();
this.Label6.Text = dv.Table.Rows[0]["monthoutputs"].ToString();
}
catch(Exception err)
{
Page.RegisterStartupScript("tip","<script>alert('"+err.Message+"')</script>");
return;
}

}
gongsun 2008-12-01
  • 打赏
  • 举报
回复
楼主开了2贴??

汗....晕....

建议楼主学习 castle...

这样lz你就不用为那些服务器控件而烦恼了,再也不用去管什么viewstate...

zjybushiren88888 2008-12-01
  • 打赏
  • 举报
回复
dt.Rows[行索引][列索引] 来获取指定行指定列的值
路人乙e 2008-12-01
  • 打赏
  • 举报
回复
ds.Tables[0].Rows[0]["UserName"].ToString()
mrhu7002 2008-12-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yfqvip 的回复:]
C# codepublic Page_Load(....)
{
if(!IsPostBack)
{
string strSql = "select Id,UserName,Password,sex,age,Tel,Email from user where id=1";
DataSet ds = Query(strSql);
if(ds!=null&&ds.Tables[0].Rows.Count>0)
{
txtUserName.text= ds.Tables[0].Rows[0]["UserName"];
txtAge.text= ds.Tables[0].Rows[0]["age"];
txtsex.text=ds.Tables[0].Row…
[/Quote]

正解!
jiang_jiajia10 2008-12-01
  • 打赏
  • 举报
回复
用DataRow也可以

foreach(DataRow dr in dt)
{
txtUserName.Text= dr["UserName"];
txtAge.Text= dr["age"];
txtsex.Text=dr["sex"];
}
lovehongyun 2008-12-01
  • 打赏
  • 举报
回复
SqlDataAdapter da = new SqlDataAdapter("select Id,UserName,Password,sex,age,Tel,Email from user where id="+id,conn)
DataTable dt = new DataTable();
da.Fill(dt);
txtUserName.Text= dt.Rows[0]["UserName"];
txtAge.Text= dt.Rows[0]["age"];
txtsex.Text=dt.Rows[0]["sex"];
orain 2008-12-01
  • 打赏
  • 举报
回复
如果是多行,在 1 楼的程序上作一下修改:

if(ds != null && ds.Tables.Count > 0)
{
DataTable dt = ds.Table[0];
DataRow[] drs = dt.Select("id = 5"); //这里写你的过滤条件,类似于 SQL 的 Where 子句
if(drs != null)
{
txtUserName.text = drs[0]["UserName"].ToString();
txtUserName.text = drs[0]["Age"].ToString();
txtUserName.text = drs[0]["Sex"].ToString();
}
}
lovehongyun 2008-12-01
  • 打赏
  • 举报
回复
直接将你的数据填充到DataTable中

然后可通过 dt.Rows[行索引][列索引] 来获取指定行指定列的值
hy_lihuan 2008-12-01
  • 打赏
  • 举报
回复
txtUserName.text= ds.Tables[0].Rows[0]["UserName"];
txtAge.text= ds.Tables[0].Rows[0]["age"];
txtsex.text=ds.Tables[0].Rows[0]["sex"];
greystar 2008-12-01
  • 打赏
  • 举报
回复
DataTable 就相当于数据库的表,可以通过rows[i][j]来访问
Apple 2008-12-01
  • 打赏
  • 举报
回复
一楼~~up
whwhzzz 2008-12-01
  • 打赏
  • 举报
回复
string sql = "select * from Lines where dataset=@dataset and tag=@tag order by [left]";
SqlCommand sqlcmd = new SqlCommand(sql, SqlConn);
if (SqlConn.State == ConnectionState.Closed)
SqlConn.Open();

SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = sqlcmd;
DataSet daLines=new DataSet();
sda.Fill(daLines);
List<Line> LinesFromDB=new List<Line>();

txtUserName.text= daLines.Tables[0].Rows[0]["UserName"];
txtAge.text= daLines.Tables[0].Rows[0]["age"];
txtsex.text=daLines.Tables[0].Rows[0]["sex"];

满衣兄 2008-12-01
  • 打赏
  • 举报
回复
public Page_Load(....)
{
if(!IsPostBack)
{
string strSql = "select Id,UserName,Password,sex,age,Tel,Email from user where id=1";
DataSet ds = Query(strSql);
if(ds!=null&&ds.Tables[0].Rows.Count>0)
{
txtUserName.text= ds.Tables[0].Rows[0]["UserName"];
txtAge.text= ds.Tables[0].Rows[0]["age"];
txtsex.text=ds.Tables[0].Rows[0]["sex"];
}
}
}
public static string connectionString = "..................";
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}


也可以用DataReader,方法很多,建议lz还是学习一下在去做东西.

62,268

社区成员

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

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

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

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