从数据库SELECT一个数据,怎么把它赋值给变量? 偶好象记得一点,看对不对连接数据库;查询数据并填充到DS;int i;i=ds.Tables["表名"].Rows[0];这样不对是吧,相应的代码该怎么写呢,请高手指教。谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 i=Convert.ToInt32(ds.Tables["表名"].Rows[0][0]);第一行的第一列 if(!IsPostBack) { string dsn = ConfigurationSettings.AppSettings["TRS"]; SqlConnection myConnection = new SqlConnection(dsn); string mySelectQuery="select a.shopcode,a.shopname,a.shopchief,a.shopphone,a.shopfax, a.setupdate,a.memo,a.shopaddress,b.areaname from Shopdata as a left outer join area as b on a.areacode=b.areacode where a.ShopCode='"+ShopCode+"'"; SqlCommand cmd=new SqlCommand(mySelectQuery,myConnection) ; myConnection.Open (); SqlDataReader dr=cmd.ExecuteReader(); while(dr.Read()) { lbCode.Text=dr["shopcode"].ToString(); txtName.Text=dr["shopname"].ToString(); txtChief.Text=dr["shopchief"].ToString(); DateTimeBox1.Text =dr["setupdate"].ToString(); ddlArea.SelectedItem.Text=dr["areaname"].ToString(); txtAddress.Text=dr["shopaddress"].ToString(); txtFax.Text=dr["shopfax"].ToString(); txtMemo.Text=dr["memo"].ToString(); txtPhone.Text =dr["shopphone"].ToString(); } dr.Close (); cmd.Dispose (); myConnection.Close (); }这是我得赋值,希望楼主能用的上 string s = ds.Tables["表名"].Rows[0]["列名"].tostring();int i=System.convert.toint32(ds.Tables["表名"].Rows[0]["列名"].tostring()); SqlCommand.ExecuteScalar 方法 [C#]执行查询,并返回查询所返回的结果集中第一行的第一列。忽略额外的列或行。cmd.CommandText = "SELECT COUNT(*) FROM 表名 WHERE Sex='男'";Int32 count = (int32) cmd.ExecuteScalar();我的意思是说要用SqlCommand对象的ExecuteScalar 方法,sql语句假设是得到所有男生的人数:SELECT COUNT(*) FROM 表名 WHERE Sex='男'然后显示在页面上可以加个Label:Label1.Text=count.ToString(); string s=ds.Tables["表名"].Rows[0][0].ToString();然后转化再用 楼上的可以。也可以用enum得到Rows的值,然后赋予变量。IEnumerator a=dataSet1.Tables["表名"].DefaultView.Table.Rows.GetEnumerator();while(a.MoveNext()){ row=(DataRow)a.Current; string i = row[1].ToString(); ......} string col=ds.Tables["表名"].Rows[0][0].ToString(); luckyTOTO(胡晓强)代码比较标准哦,shoutor(土人制造) 的代码可以实现了。 i=ds.Tables["表名"].Rows(0).item("列名") int i=Convert.ToInt32(ds.Tables["TableName"].Rows[row][cell]); 晕,搞得这么麻烦,SqlCommand.ExecuteScalar 方法就返回一个值,连数据集都不用填充 急:历treeview下所有的结点的问题 vs2005中的asp.net development server在属性窗口找不到虚拟路径 关于 ~/ 符号的问题 datatable的primarykeycolumns的问题 asp.net页面经常出错 一个ODBC连接问题!!高手指点!! asp.net的网站如何做二级域名啊,谢谢! 菜鸟散分啦 【高分悬赏】WEB登陆页面POST提交数据Form Data中存有账号和明文密码。这样做的风险 急、急、急)帮我看看哪有问题 池连接已被使用问题 请问一下在DataList中放RadioButtonList如何Coding?
第一行的第一列
{
string dsn = ConfigurationSettings.AppSettings["TRS"];
SqlConnection myConnection = new SqlConnection(dsn);
string mySelectQuery="select a.shopcode,a.shopname,a.shopchief,a.shopphone,a.shopfax, a.setupdate,a.memo,a.shopaddress,b.areaname from Shopdata as a left outer join area as b on a.areacode=b.areacode where a.ShopCode='"+ShopCode+"'";
SqlCommand cmd=new SqlCommand(mySelectQuery,myConnection) ;
myConnection.Open ();
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
lbCode.Text=dr["shopcode"].ToString();
txtName.Text=dr["shopname"].ToString();
txtChief.Text=dr["shopchief"].ToString();
DateTimeBox1.Text =dr["setupdate"].ToString();
ddlArea.SelectedItem.Text=dr["areaname"].ToString();
txtAddress.Text=dr["shopaddress"].ToString();
txtFax.Text=dr["shopfax"].ToString();
txtMemo.Text=dr["memo"].ToString();
txtPhone.Text =dr["shopphone"].ToString();
}
dr.Close ();
cmd.Dispose ();
myConnection.Close ();
}这是我得赋值,希望楼主能用的上
int i=System.convert.toint32(ds.Tables["表名"].Rows[0]["列名"].tostring());
执行查询,并返回查询所返回的结果集中第一行的第一列。忽略额外的列或行。
cmd.CommandText = "SELECT COUNT(*) FROM 表名 WHERE Sex='男'";
Int32 count = (int32) cmd.ExecuteScalar();我的意思是说要用SqlCommand对象的ExecuteScalar 方法,sql语句假设是得到所有男生的人数:SELECT COUNT(*) FROM 表名 WHERE Sex='男'然后显示在页面上可以加个Label:
Label1.Text=count.ToString();
也可以用enum得到Rows的值,然后赋予变量。
IEnumerator a=dataSet1.Tables["表名"].DefaultView.Table.Rows.GetEnumerator();
while(a.MoveNext())
{
row=(DataRow)a.Current;
string i = row[1].ToString();
......
}
晕,搞得这么麻烦,SqlCommand.ExecuteScalar 方法就返回一个值,连数据集都不用填充