树视图数据到数据库 [英] treeview data to database
本文介绍了树视图数据到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在开发一个项目,其中有一个树状视图,并且它是从数据库中填充的,我想在单击按钮后将树状视图中的选中项插入数据库中,我没有得到如何在树状视图中插入选中的项到我的按钮单击事件中的代码如下所示.谁能帮我...
Hello all,
I am developing a project in which I am having a treeview and it is populated from the database, I want to insert the checked item in treeview to database after clicking a button, I am not getting how to insert the checked item in tree view to database.My code in button click event looks like this. Can any one help me...
protected void btn1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=GAYATHRI\SQLEXPRESS;Initial Catalog=VOY_SERVICES1;Integrated Security=True"); con.Open();
foreach (TreeNode tnode in TreeView1.CheckedNodes)
{
if (tnode.Checked == true)
{
string strTreeValue;
strTreeValue = tnode.Value;
SqlCommand com = new SqlCommand("insert into treeview_select(subdisease_name) values(@name)", con);
com.Parameters.AddWithValue("@name", strTreeValue);
com.ExecuteNonQuery();
con.Close();
}
}
}
[edit]已添加代码块,已删除紧急性-OriginalGriff [/edit]
[edit]Code block added, Urgency deleted - OriginalGriff[/edit]
推荐答案
乍一看,您的代码似乎还可以,但是...
On the first look, youre code seems to be OK, but...
protected void btn1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=GAYATHRI\SQLEXPRESS;Initial Catalog=VOY_SERVICES1;Integrated Security=True"); con.Open();
foreach (TreeNode tnode in TreeView1.CheckedNodes)
{
if (tnode.Checked == true)
{
string strTreeValue;
strTreeValue = tnode.Value;
SqlCommand com = new SqlCommand("insert into treeview_select(subdisease_name) values(@name)", con);
com.Parameters.AddWithValue("@name", strTreeValue);
com.ExecuteNonQuery();
com.Dispose();
//con.Close(); if connection is closed, the command will be executed only once!
}
}
con.Close();
com = Nothing;
con = Nothing;
}
祝你有美好的一天;)
Have a nice day ;)
是的,永久链接,确保,//con.Close();如果关闭连接,该命令将仅执行一次!...
yes,Permalink,sure, //con.Close(); if connection is closed, the command will be executed only once!...
这篇关于树视图数据到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文