private int GetStarId()
{
DBHelper db = new DBHelper();
int StarId = (int)db.GetScalar(string.Format("select Id from Star where Star='{0}'", this.cboStar.Text));
db.DBClose();
return StarId;
}
if (this.cboStar.Text != "")
{
StarId = this.cboStar.SelectedIndex;
}
else
{
StarId = DBNull.Value;
} sql = string.Format("insert into Users(StarId) values('{0}')", StarId);如果cboStar的Text没有值的话,我想让StarId的值为空,但是StarId为空了插入数据库的时候又会报错,数据库里StarId的类型是Int的,且可以为空,大家帮帮忙呀~~~
{
DBHelper db = new DBHelper();
int StarId = (int)db.GetScalar(string.Format("select Id from Star where Star='{0}'", this.cboStar.Text));
db.DBClose();
return StarId;
}
if (this.cboStar.Text != "")
{
StarId = this.cboStar.SelectedIndex;
}
else
{
StarId = DBNull.Value;
} sql = string.Format("insert into Users(StarId) values('{0}')", StarId);如果cboStar的Text没有值的话,我想让StarId的值为空,但是StarId为空了插入数据库的时候又会报错,数据库里StarId的类型是Int的,且可以为空,大家帮帮忙呀~~~
if(StarId.Trim()!="")
sql = string.Format("insert into Users(StarId) values('{0}')", StarId);
else
sql = string.Format("insert into Users(StarId) values('{0}')", "null");或者采用Sql参数,用参数最合适.
esle