日期:2014-05-18  浏览次数:22157 次

ExecuteNonQuery: CommandText 属性尚未初始化
C# code


        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();
            }




问题如题



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