大家好,又见面了,我是你们的朋友全栈君。
面试的时候一个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>”); } } }
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/161244.html原文链接:https://javaforall.cn