字符串转换的问题?

xpfoo2009 2010-01-01 06:45:13
要实现查询表中的某一字段的数字插入到 textBoxbrno 中,代码如下:
private void Form1_Load(object sender, EventArgs e)
{
try
{
string strsqlconn = "server= (local);database = AIS20090413161510;trusted_connection = yes";
SqlConnection conn = new SqlConnection(strsqlconn);
string str = "Select famount from t_voucherentry where fvoucherid = 185 and fentryid =0 ";
conn.Open();
int a;
a = Int32.Parse(str);
textBoxbrno.Text = a.ToString();

SqlCommand cmd = new SqlCommand(str, conn);

cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("over");

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
报错:输入字符串的格式不正确。何解?
...全文
149 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyq11 的回复:]
ExecuteScalar语句返回的是影响数目
SELECT COUNT(*) from t_voucherentry
[/Quote]

你记错了

是返回首行首列数据
wuyq11 2010-01-01
  • 打赏
  • 举报
回复
ExecuteScalar语句返回的是影响数目
SELECT COUNT(*) from t_voucherentry
xpfoo2009 2010-01-01
  • 打赏
  • 举报
回复
private void Form1_Load(object sender, EventArgs e)
{
try
{
string strsqlconn = "server= (local);database = AIS20090413161510;trusted_connection = yes";
SqlConnection conn = new SqlConnection(strsqlconn);
string str = "Select famount from t_voucherentry where fvoucherid = 185 and fentryid =0 ";
conn.Open();
SqlCommand cmd = new SqlCommand(str, conn);
int a = (int)cmd.ExecuteScalar();
conn.Close();
textBoxbrno.Text = a.ToString();
MessageBox.Show("over");

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
提示:指定的转换无效。为什么呢?
  • 打赏
  • 举报
回复
SqlCommand cmd = new SqlCommand(str, conn);

int a =(int)cmd.ExecuteScalar();
conn.Close();
textBoxbrno.Text = a.ToString();

你要加油
wuyq11 2010-01-01
  • 打赏
  • 举报
回复
str是字符串,要先执行再取值
ussing(SqlConnection conn = new SqlConnection(strsqlconn))
{
conn.Open();
SqlCommand cmd = new SqlCommand(Sql, Connection);
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Reader())
{

}
Connection.Close();
}
xpfoo2009 2010-01-01
  • 打赏
  • 举报
回复
famount是money型的, 应该如何做呢?
threenewbee 2010-01-01
  • 打赏
  • 举报
回复
a = Int32.Parse(str);
str显然不是一个整数。

111,120

社区成员

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

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

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