日期:2014-05-18 浏览次数:21107 次
using System.Data.SqlClient;//SQl的程序集 //connectionstring是连接数据库的字符串 SqlConnection coon = new SqlConnection(connectionstring); coon.Open(); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(); SqlCommand cmd = new SqlCommand("select * from 酒店人员", coon); da.SelectCommand = cmd; da.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; coon.Close();
------解决方案--------------------
treeView1.ShowLines = true;
treeView1.ImageList = imageList1;//server=.;integrated security=sspi;database=db_02
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123;database=db_02");
con.Open();
SqlCommand com = new SqlCommand("select * from tb_07", con);
SqlDataReader dr = com.ExecuteReader();
TreeNode newNode1 = treeView1.Nodes.Add("A","商品信息",1,2);//一级节点
while (dr.Read())
{
TreeNode newNode12 = new TreeNode("商品编号" + dr[1].ToString(),3,4);// 二级节点
// 3表示没有单击节点时显示的图标索引值,
newNode12.Nodes.Add("A","商品名称:" + dr[0].ToString(),5,6);
newNode12.Nodes.Add("A","商品数量:" + dr[3].ToString(),7,8);
newNode12.Nodes.Add("A", "商品价格:" + dr[2].ToString(),9,10);
newNode1.Nodes.Add(newNode12);
}
dr.Close();
con.Close();
treeView1.ExpandAll();
}
------解决方案--------------------
private void bindTreeView1() { string sql = "select * from dm_category"; DataTable dt = db.ExecuteDataTable(sql, CommandType.Text, null); DataRow[] dr = dt.Select("parent_id is null"); for (int i = 0; i < dr.Length; i++) { TreeNode tn = new TreeNode(); tn.Text = dr[i]["category_name"].ToString(); tn.Tag = dr[i]["id"].ToString(); FillTree(tn, dt); treeView1.Nodes.Add(tn); } } private void FillTree(TreeNode node, DataTable dt) { DataRow[] drr = dt.Select("parent_id='" + node.Tag.ToString() + "'"); if (drr.Length > 0) { for (int i = 0; i < drr.Length; i++) { TreeNode tnn = new TreeNode(); tnn.Text = drr[i]["category_name"].ToString(); tnn.Tag = drr[i]["id"].ToString(); if (drr[i]["parent_id"].ToString() == node.Tag.ToString()) { FillTree(tnn, dt); } node.Nodes.Add(tnn); } } } private void 删除节点ToolStripMenuItem_Click(object sender, EventArgs e) { treeView1.Nodes.Remove(treeView1.SelectedNode); N_conn.Open(); string str = "delete from Ware where 名称='"+treeView1.SelectedNode.Text+"' "; SqlCommand cmd = new SqlCommand(str,N_conn); cmd.ExecuteNonQuery(); N_conn.Close(); } treeview1.Nodes.Add(node);
------解决方案--------------------