public partial class Fr_Main : Form
    {
        //SqlConnection objsqlconnection;
        //SqlDataAdapter objsqldataadapter;
        private  SqlConnection objsqlconnection;
        private  SqlCommand objsqlcommand;
        private  string inscmd;
        private  string   sun;
  
        public Fr_Main()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“baidatongDataSet.h_xiaofeijilu”中。您可以根据需要移动或移除它。
            this.h_xiaofeijiluTableAdapter.Fill(this.baidatongDataSet.h_xiaofeijilu);
            //链接金蝶数据库
            objsqlconnection = new SqlConnection("server =61.150.91.149;database = baidatong;uid =baidatong_f;pwd =cbG62lQIlZ ");
     
           sun ="select sum(xiaofeijine) from h_xiaofeijilu";
           suu = (int)sun;

string 无法转换为 int 
提供的两种方法都用了
  int.Parse()   
System.Convert.ToIntXX() 都不行
高手有好办法吗?

           

解决方案 »

  1.   

    lz去看看sql操作的基础吧,这不是string转换int的问题
      

  2.   


    我倒,你都没有执行SQL语句,sun 就一个字符串怎么可能转换成int...
      

  3.   

    你要这样 
    private void Form1_Load(object sender, EventArgs e) 
            { 
                // TODO: 这行代码将数据加载到表“baidatongDataSet.h_xiaofeijilu”中。您可以根据需要移动或移除它。 
                this.h_xiaofeijiluTableAdapter.Fill(this.baidatongDataSet.h_xiaofeijilu); 
                //链接金蝶数据库 
                objsqlconnection = new SqlConnection("server =61.150.91.149;database =baidatong;uid =baidatong_f;pwd =cbG62lQIlZ "); 
    sun ="select sum(xiaofeijine) from h_xiaofeijilu"; 
    SqlCommand cmd = new SqlCommand(sun,objsqlconnection);
    objsqlconnection.Open();
    suu=Convert.ToInt32(cmd.ExecuteScalar());
    objsqlconnection.Close();
      

  4.   

    LZ先熟悉一下ado.net,看看怎么执行sql语句及返回结果
      

  5.   

    字符串怎么可能转为int
    string sql = "select sum(xiaofeijine) from h_xiaofeijilu"; 
    int count = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.Conn, CommandType.Text, sql)); 
    if (count > 0) 
        flag = true; else 
        flag = false; using(SqlConnection thisConnection = new SqlConnection(@"")) 
    { thisConnection.Open(); 
    SqlCommand cmd= thisConnection.CreateCommand(); 
    thisCommand.CommandText = "select sum(xiaofeijine) from h_xiaofeijilu" 
    object countResult =cmd.ExecuteScalar(); thisConnection.Close(); 

      

  6.   

     sun ="select sum(xiaofeijine) from h_xiaofeijilu"; 
              suu = (int)sun; 
    晕  没执行那
      

  7.   

    把数据取到datatable,再取出来用
      

  8.   


    收集结果的sun换一个新定义的int型变量
      

  9.   

    假以时日,楼主必是linq2sql的高手...
      

  10.   

    sun ="select sum(xiaofeijine) from h_xiaofeijilu"; 先执行Sql语句,才能获取啊
      

  11.   

    int D=0;
    int.TryParse("你要转换的字符串",out D);
    转换失败时输出D,成功就不用说了
      

  12.   

    哦这个不能这样,人实在,问题也比较简单,要是急着开发,可以用一些代码生成器,可以帮你把底层的东西解决一点,看一下生成的东西,学习,不急了可以学习一下ado.net连接数据库的一种技术。加油!
      

  13.   

    真的很感谢CSDN上的朋友,我刚毕业,做金蝶代理的实施人员,接一个二次开发的项目,没人做。硬着头皮接下来。从来没有做过开发,就这样一点一点的啃。没有老师,挺困难的,时间又催的紧。多靠大家一步一步帮我,谢谢各位大哥大姐。没有你们,我真的不知道怎么办,谢谢各位了。