private void SaveSongInfo()
        {
            int songWordCount = this.txtSongName.Text.Length;
            StringBuilder sb = new StringBuilder();
            #region 判定窗体类型,确定是修改或者新增
            
            /*新增歌曲信息*/
            if (frmType.Equals("addSong"))
            {                sb.AppendLine("insert song_info(song_name,song_ab,song_word_count,songtype_id,singer_id,song_url,song_play_count)");
                sb.AppendFormat("values('{0}','{1}',{2},{3},{4},'{5}',default)",
                    txtSongName.Text, txtSpell.Text, songWordCount, cboSongType.SelectedValue, singerId, txtSongFileName.Text);
                             
            }
            /*修改歌曲信息SQL语句*/
            else if (frmType.Equals("updateSong"))
            {
               
               
            
            }
            #endregion         
            SqlCommand command = new SqlCommand(sql, DBHelper.conn);
            try
            {
                DBHelper.conn.Open();
                int ret = (int)command.ExecuteNonQuery();
                if (ret >= 0)
                {
                    MessageBox.Show("保存成功", "提示");
                }
                else
                {
                    MessageBox.Show("保存失败", "提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }
            finally 
            {
                DBHelper.conn.Close();
            }问题如题

解决方案 »

  1.   

    command 下的CommandText属性要赋值。连接字符串!
      

  2.   

    SqlCommand command = new SqlCommand(sql, DBHelper.conn);
    红字部分,我觉得你应该换成
    SqlCommand command = new SqlCommand(sb.ToString(), DBHelper.conn);
      

  3.   

    问题解决,之前我本来没用 StringBuilder,忘记改了。
      

  4.   

     SqlCommand command = new SqlCommand(sql, DBHelper.conn);
    以上的语句中,缺少sql的查询语句,你还没赋值呢,当然没有
      

  5.   


     else if (frmType.Equals("updateSong"))
                {
                    GetSingerID(this.txtsinger.Text.ToString());
                    sb.AppendLine("update  [song_info] ");
                    sb.AppendFormat("set  song_name='{0}',song_ab='{1}',song_word_count='{2}',songtype_id='{3}',singer_id='{4}',song_url='{5}',song_play_count=default"
                        ,txtSongName.Text, txtSpell.Text, songWordCount, cboSongType.SelectedValue,singerId, txtSongFileName.Text);
                    sb.AppendFormat(" where  song_name='{0}'", txtSongName.Text);
                    Console.WriteLine("//" + sb.ToString());
                }
    增加功能已解决。帮我看看修改哪错了。
    “未将对象引用设置到对象的实例”
      

  6.   

    你可以改成
    string sql="";
    sql="insert song_info(song_name,song_ab,song_word_count,songtype_id,singer_id,song_url,song_play_count)";
    sql+="values('{0}','{1}',{2},{3},{4},'{5}',default)",txtSongName.Text, txtSpell.Text, songWordCount, cboSongType.SelectedValue, singerId, txtSongFileName.Text);
                  
      

  7.   

    断点,看看 cboSongType.SelectedValue 是不是 null
      

  8.   

    update [song_info] set song_name='1',song_ab='2',song_word_count='3',songtype_id='4',singer_id='5',song_url='fgfg',song_play_count=default where song_name='1' 打印出来没事。那就是下拉框值没取得。
      

  9.   

    cboSongType.SelectedValue 正常  等于1
      

  10.   

    哪一行出得错?
    把断点设在
    else if (frmType.Equals("updateSong"))
    下面的大括号上,然后调试停到那,下面代码里那些控件名都用鼠标移上去,看看哪个是 null 的
      

  11.   

    问题很明确,楼主看看吧,代码走到报错那里的时候,sql是空值(或者null)
      

  12.   


       else if (frmType.Equals("updateSong"))
                {
                    GetSingerID(this.txtsinger.Text.ToString());
                    sb.AppendLine("update  [song_info] ");
                    sb.AppendFormat("set  song_name='{0}',song_ab='{1}',song_word_count='{2}',songtype_id='{3}',singer_id='{4}',song_url='{5}',song_play_count=default"
                        , txtSongName.Text, txtSpell.Text, songWordCount, cboSongType.SelectedValue, singerId, txtSongFileName.Text);
                    sb.AppendFormat(" where  song_name='{0}'", txtSongName.Text);                sb.AppendFormat("update");                          
                
                }其他都值,唯一第五个singerId 为0
      

  13.   


      public bool GetSingerID(string singerName)
            {
                bool ret = false;
                this.txtsinger.Text = singerName;
                StringBuilder sbSql = new StringBuilder();
                sbSql.AppendFormat("select singer_id from singer_info where singer_name='{0}'",singerName);
                try
                {
                    DBHelper.conn.Open();
                    SqlCommand command = new SqlCommand(sbSql.ToString(),DBHelper.conn);
                    singerId = (int)command.ExecuteScalar();
                    if (singerId > 0)
                    {
                        ret = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示");
                }
                finally 
                {
                    DBHelper.conn.Close();
                }
                return ret;
            }SingerID得到的方法。得到后等于0