DropDownList1对数据库的操作「建议收藏」

发布时间:2025-12-09 14:06:34 浏览次数:4

面试的时候一个DropDownList1控件对数据库的增加难住了,翻翻以前做过的项目,原来如此的简单,晒出来分享一下。

1.添加数据

//得到数据dataset

public void bind(string sql)
{

string st = @”Data Source=PC-20140331BMRR\SQLEXPRESS;Initial Catalog=lianxi;Integrated Security=True”;
SqlConnection cn = new SqlConnection(st);
SqlCommand cm = new SqlCommand(sql,cn);
SqlDataAdapter dr = new SqlDataAdapter(cm);
DataTable da = new DataTable();
dr.Fill(da);


}

//向DropDownList1增加
public void add()
{

string sql = “insert into zy values(‘”+DropDownList1.SelectedItem.ToString().Trim()+”‘,'”+DropDownList2.SelectedItem.ToString ().Trim()+”‘)”;
bind(sql);
}

protected void Button1_Click(object sender, EventArgs e)
{

try { add();

Response.Write(“<script>javascript:alert(‘提交成功’)</script>”);

}
catch { Response.Write(“<script>javascript:alert(‘提交失败’)</script>”); }



}
}

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477