SqlConnection方法取变值 并赋值给变量

pangle0225 2012-07-08 02:32:27
在asp.net中用SqlConnection方法取变量时,代码如下:

SqlConnection connect = new SqlConnection("server=localhost;uid=sa;password=;database=StudentsInfo");
connect.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
string s = "select PW from User1 where ID='123'";
adapter.SelectCommand = new SqlCommand(s, connect);
DataSet ds = new DataSet();
adapter.Fill(ds);
string s1 = ds.Tables[0].Rows[0][0];


表User1 中有ID 和PW两条属性
想把ID为“123”的PW属性赋给s1,为什么在赋值语句中有错误?
显示无法将类型“object”隐式的转换为“string”类型



新手,急求~~
...全文
114 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pangle0225 2012-07-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

C# code

SqlConnection connect = new SqlConnection("server=localhost;uid=sa;password=;database=StudentsInfo");
connect.Open();
string s = "select PW from User1 where ID='123'";
SqlDataAdapter ……
[/Quote]


可以了啊 大神啊 但是这是什么原因呢
暖枫无敌 2012-07-08
  • 打赏
  • 举报
回复

SqlConnection connect = new SqlConnection("server=localhost;uid=sa;password=;database=StudentsInfo");
connect.Open();
string s = "select PW from User1 where ID='123'";
SqlDataAdapter adapter = new SqlDataAdapter(s,connect);
DataSet ds = new DataSet();
adapter.Fill(ds);
string s1 = ds.Tables[0].Rows[0]["PW"].ToString();
pangle0225 2012-07-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

C# code

string s1 = ds.Tables[0].Rows[0][0].ToString();
[/Quote]

这个也是不行啊 还是错误
说说的是无法将方法组“toString()转换为非委托类型“string”;”
pangle0225 2012-07-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

C# code

string s1 = ds.Tables[0].Rows[0][0].ToString();
[/Quote]

这个也是不行啊 还是错误
说说的是无法将方法组“toString()转换为非委托类型“string”;”
人生无悔 2012-07-08
  • 打赏
  • 举报
回复

string s1 = ds.Tables[0].Rows[0][0].ToString();
mizuho_2006 2012-07-08
  • 打赏
  • 举报
回复
string s1 = ds.Tables[0].Rows[0][0].ToString();

DataSet取出的值默认是object类型,要转换。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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