要实现查询表中的某一字段的数字插入到 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);
            }            
        }
报错:输入字符串的格式不正确。何解?

解决方案 »

  1.   

    a = Int32.Parse(str); 
    str显然不是一个整数。
      

  2.   

     famount是money型的, 应该如何做呢?
      

  3.   

    str是字符串,要先执行再取值
    ussing(SqlConnection conn = new SqlConnection(strsqlconn))
    {
    conn.Open();
    SqlCommand cmd = new SqlCommand(Sql, Connection); 
    SqlDataReader reader = cmd.ExecuteReader(); 
    if(reader.Reader())
    {
     
    }
    Connection.Close(); 
    }
      

  4.   

     SqlCommand cmd = new SqlCommand(str, conn);             int a =(int)cmd.ExecuteScalar(); 
                    conn.Close(); 
    textBoxbrno.Text = a.ToString(); 你要加油
      

  5.   

     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);
                }            
            }
    提示:指定的转换无效。为什么呢?
      

  6.   

    ExecuteScalar语句返回的是影响数目
    SELECT COUNT(*) from t_voucherentry