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();
}问题如题
{
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(sb.ToString(), DBHelper.conn);
以上的语句中,缺少sql的查询语句,你还没赋值呢,当然没有
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());
}
增加功能已解决。帮我看看修改哪错了。
“未将对象引用设置到对象的实例”
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);
把断点设在
else if (frmType.Equals("updateSong"))
下面的大括号上,然后调试停到那,下面代码里那些控件名都用鼠标移上去,看看哪个是 null 的
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
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