用dropdownlist在数据库里插入一个空值

偶然看到“有关:this.DropDownList1.Items.Insert(0,"请选择");”的一个贴子。
在回复中看到reeezak的解决办法:
string sql = "insert into tab (col1) values (";
string myid = this.DropDownList1.SelectedValue;
if(this.DropDownList1.SelectedItem.Text=="请选择")
{
sql += "NULL)";
}
else
{
sql += myid + ")";
}

好看点的,可以写成
const string SQL = "insert into tab (col1) values ({0})";
string mysql;
string myid = this.DropDownList1.SelectedValue;
if(this.DropDownList1.SelectedItem.Text=="请选择")
{
mysql = String.FOrmat(SQL, "NULL");
}
else
{
mysql = String.FOrmat(SQL, myid);
}
简简单单的解决了问题。

posted on 2007-08-31 10:34  Crazy石头  阅读(389)  评论(0编辑  收藏  举报

导航