如果你是在webform中,你可以根据你的选择重新打开一个页面,在winform中,你可以重新绑定数据!

解决方案 »

  1.   

    我想在TREEVIEW得节点双击事件中,根据他的值和父节点的值,生成一个连接字符串,然后用这个字符串连接数据库,用select * from 表 得到相应表的数据。
      

  2.   


     exec 'select * from '+  父节点的值(库名) + '..' + 子节点(表名) 
    数据生成dataset 双击子节点时执行 语句和刷新dataset
      

  3.   

    copy上来,看下么,大家都可以改改呀,这么好的事,上那里找!~
      

  4.   

    SQLDMOHelper.cs:
    using System;
    using System.Collections;
    using SQLDMO;namespace Bluegrass.Data
    {
    /// <summary>
    /// Provides helper functions for SQLDMO
    /// </summary>
    public class SQLDMOHelper
    {
    public string ServerName;
    public string UserName;
    public string Password;
    public string Database;
    public string Table; /// <summary>
    /// SQLDMO.SQLServer Connection
    /// </summary>
    private SQLDMO.SQLServer Connection = new SQLDMO.SQLServerClass(); /// <summary>
    /// Connects this.Connection
    /// </summary>
    public void Connect()
    {
    Connection.Connect(ServerName, UserName, Password);
    } /// <summary>
    /// DisConnects this.Connection
    /// </summary>
    public void DisConnect()
    {
    Connection.DisConnect();
    } /// <summary>
    /// An array of Registered SQL Servers
    /// </summary>
    /// <res>
    /// Thanks to Leppie @ CodeProject for the following
    /// </res>
    public Array RegisteredServers
    {
    get
    {
    ArrayList aServers = new ArrayList();
    SQLDMO.ApplicationClass acServers = new SQLDMO.ApplicationClass(); for (int iServerGroupCount = 1; iServerGroupCount <= acServers.ServerGroups.Count; iServerGroupCount++)
    for (int iServerCount = 1; iServerCount <= acServers.ServerGroups.Item(iServerGroupCount).RegisteredServers.Count; iServerCount++)
    aServers.Add(acServers.ServerGroups.Item(iServerGroupCount).RegisteredServers.Item(iServerCount).Name); return aServers.ToArray();
    }
    } /// <summary>
    /// An array of Databases in a SQL Server
    /// </summary>
    public Array Databases
    {
    get
    {
    ArrayList aDatabases = new ArrayList(); foreach(SQLDMO.Database dbCurrent in Connection.Databases)
    aDatabases.Add(dbCurrent.Name); return aDatabases.ToArray();
    }
    } /// <summary>
    /// Array of Tables in a Database
    /// </summary>
    public Array Tables
    {
    get
    {
    ArrayList aTables = new ArrayList();
    SQLDMO.Database dbCurrent = (SQLDMO.Database)Connection.Databases.Item(this.Database, Connection); foreach(SQLDMO.Table tblCurrent in dbCurrent.Tables)
    aTables.Add(tblCurrent.Name);

    return aTables.ToArray();
    }
    } /// <summary>
    /// A SQLDMO.Columns collection of Fields (Columns) in a Table
    /// </summary>
    public SQLDMO.Columns Fields
    {
    get
    {
    SQLDMO.Database dbCurrent = (SQLDMO.Database)Connection.Databases.Item(this.Database, Connection);
    SQLDMO.Table tblCurrent = (SQLDMO.Table)dbCurrent.Tables.Item(this.Table, Connection); return tblCurrent.Columns;
    }
    }
    }
    }
      

  5.   

    form1.cs.1:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Configuration;
    using Bluegrass.Data;namespace SPGen
    {
    public class frmMain : System.Windows.Forms.Form
    {
    protected SQLDMOHelper dmoMain = new SQLDMOHelper(); private System.Windows.Forms.StatusBar statbarMain;
    private System.Windows.Forms.StatusBarPanel statbarpnlMain;
    private System.Windows.Forms.Button cmdConnect;
    private System.Windows.Forms.TextBox txtPassword;
    private System.Windows.Forms.TextBox txtUser;
    private System.Windows.Forms.ComboBox selServers;
    private System.Windows.Forms.Splitter spltrMain;
    private System.Windows.Forms.Panel pnlConnectTo;
    private System.Windows.Forms.TreeView tvwServerExplorer;
    private System.Windows.Forms.ImageList imglstMain;
    private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
    private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
    private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
    private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
    private System.Windows.Forms.DataGrid dataGrid1;
    private System.ComponentModel.IContainer components;
    public frmMain()
    {
    InitializeComponent();
    object[] objServers = (object[])dmoMain.RegisteredServers;
    selServers.Items.AddRange(objServers);
    NameValueCollection settingsAppSettings = (NameValueCollection)ConfigurationSettings.AppSettings; if (settingsAppSettings["ServerName"] != null && settingsAppSettings["ServerName"] != "")
    {
    selServers.Text = settingsAppSettings["ServerName"];
    dmoMain.ServerName = settingsAppSettings["ServerName"];
    }
    if (settingsAppSettings["UserName"] != null && settingsAppSettings["UserName"] != "")
    {
    txtUser.Text = settingsAppSettings["UserName"];
    dmoMain.UserName = settingsAppSettings["UserName"];
    }
    if (settingsAppSettings["Password"] != null && settingsAppSettings["Password"] != "")
    {
    char chPassword = '*';
    txtPassword.PasswordChar = chPassword;
    txtPassword.Text = settingsAppSettings["Password"];
    dmoMain.Password = settingsAppSettings["Password"];
    }
    }
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
      

  6.   

    1.派生自己的TreeNode,在其中自定义一个PK属性,记录节点记录的主键。
    2.在TreeView的AfterSelect事件中写代码(一定得在AfterSelect,不能在BeforeSelect中写,因为那时树节点还没被选中)
    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
        // e.Node.PK就是主键,你可以把这个作为SQL语句的参数,选择相应的记录并填充到数据集中去
    }
      

  7.   

    form1.cs.2:
    #region Windows Form Designer generated code
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmMain));
    this.statbarMain = new System.Windows.Forms.StatusBar();
    this.statbarpnlMain = new System.Windows.Forms.StatusBarPanel();
    this.pnlConnectTo = new System.Windows.Forms.Panel();
    this.cmdConnect = new System.Windows.Forms.Button();
    this.txtPassword = new System.Windows.Forms.TextBox();
    this.txtUser = new System.Windows.Forms.TextBox();
    this.selServers = new System.Windows.Forms.ComboBox();
    this.tvwServerExplorer = new System.Windows.Forms.TreeView();
    this.imglstMain = new System.Windows.Forms.ImageList(this.components);
    this.spltrMain = new System.Windows.Forms.Splitter();
    this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    ((System.ComponentModel.ISupportInitialize)(this.statbarpnlMain)).BeginInit();
    this.pnlConnectTo.SuspendLayout();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    // statbarMain
    // 
    this.statbarMain.Location = new System.Drawing.Point(0, 349);
    this.statbarMain.Name = "statbarMain";
    this.statbarMain.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
       this.statbarpnlMain});
    this.statbarMain.ShowPanels = true;
    this.statbarMain.Size = new System.Drawing.Size(729, 24);
    this.statbarMain.TabIndex = 5;
    // 
    // statbarpnlMain
    // 
    this.statbarpnlMain.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
    this.statbarpnlMain.Text = "Awaiting your orders...";
    this.statbarpnlMain.Width = 713;
    // 
    // pnlConnectTo
    // 
    this.pnlConnectTo.Controls.Add(this.cmdConnect);
    this.pnlConnectTo.Controls.Add(this.txtPassword);
    this.pnlConnectTo.Controls.Add(this.txtUser);
    this.pnlConnectTo.Controls.Add(this.selServers);
    this.pnlConnectTo.Dock = System.Windows.Forms.DockStyle.Top;
    this.pnlConnectTo.Location = new System.Drawing.Point(0, -10);
    this.pnlConnectTo.Name = "pnlConnectTo";
    this.pnlConnectTo.Size = new System.Drawing.Size(729, 45);
    this.pnlConnectTo.TabIndex = 9;
    // 
    // cmdConnect
    // 
    this.cmdConnect.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
    this.cmdConnect.Location = new System.Drawing.Point(634, 17);
    this.cmdConnect.Name = "cmdConnect";
    this.cmdConnect.Size = new System.Drawing.Size(76, 23);
    this.cmdConnect.TabIndex = 7;
    this.cmdConnect.Text = "Connect";
    this.cmdConnect.Click += new System.EventHandler(this.cmdConnect_Click);
    // 
    // txtPassword
    // 
    this.txtPassword.Location = new System.Drawing.Point(422, 17);
    this.txtPassword.Name = "txtPassword";
    this.txtPassword.Size = new System.Drawing.Size(192, 21);
    this.txtPassword.TabIndex = 6;
    this.txtPassword.Text = "Password";
    this.txtPassword.Leave += new System.EventHandler(this.txtPassword_Leave);
    this.txtPassword.Enter += new System.EventHandler(this.txtPassword_Enter);
    // 
    // txtUser
    // 
    this.txtUser.Location = new System.Drawing.Point(221, 17);
    this.txtUser.Name = "txtUser";
    this.txtUser.Size = new System.Drawing.Size(192, 21);
    this.txtUser.TabIndex = 5;
    this.txtUser.Text = "User";
    this.txtUser.Leave += new System.EventHandler(this.txtUser_Leave);
    this.txtUser.Enter += new System.EventHandler(this.txtUser_Enter);
    // 
    // selServers
    // 
    this.selServers.Location = new System.Drawing.Point(10, 17);
    this.selServers.Name = "selServers";
    this.selServers.Size = new System.Drawing.Size(192, 20);
    this.selServers.TabIndex = 4;
    this.selServers.Text = "Server Name";
    this.selServers.Leave += new System.EventHandler(this.selServers_Leave);
    // 
    // tvwServerExplorer
    // 
    this.tvwServerExplorer.Dock = System.Windows.Forms.DockStyle.Left;
    this.tvwServerExplorer.FullRowSelect = true;
    this.tvwServerExplorer.ImageList = this.imglstMain;
    this.tvwServerExplorer.Location = new System.Drawing.Point(0, 35);
    this.tvwServerExplorer.Name = "tvwServerExplorer";
    this.tvwServerExplorer.Size = new System.Drawing.Size(250, 314);
    this.tvwServerExplorer.TabIndex = 10;
    this.tvwServerExplorer.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tvwServerExplorer_AfterSelect);
    this.tvwServerExplorer.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.tvwServerExplorer_BeforeExpand);
    // 
    // imglstMain
    // 
    this.imglstMain.ImageSize = new System.Drawing.Size(16, 16);
    this.imglstMain.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imglstMain.ImageStream")));
    this.imglstMain.TransparentColor = System.Drawing.Color.Transparent;
    // 
    // spltrMain
    // 
    this.spltrMain.Location = new System.Drawing.Point(250, 35);
    this.spltrMain.Name = "spltrMain";
    this.spltrMain.Size = new System.Drawing.Size(3, 314);
    this.spltrMain.TabIndex = 11;
    this.spltrMain.TabStop = false;
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(253, 35);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(476, 314);
    this.dataGrid1.TabIndex = 13;
    // 
    // frmMain
    // 
    this.AcceptButton = this.cmdConnect;
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(729, 373);
    this.Controls.Add(this.dataGrid1);
    this.Controls.Add(this.spltrMain);
    this.Controls.Add(this.tvwServerExplorer);
    this.Controls.Add(this.pnlConnectTo);
    this.Controls.Add(this.statbarMain);
    this.DockPadding.Top = -10;
    this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
    this.Name = "frmMain";
    this.Text = "SPGen: Stored Procedure Generator";
    ((System.ComponentModel.ISupportInitialize)(this.statbarpnlMain)).EndInit();
    this.pnlConnectTo.ResumeLayout(false);
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false); }
    #endregion
    [STAThread]
      

  8.   

    private void cmdConnect_Click(object sender, System.EventArgs e)
    {
    if (dmoMain.ServerName == "" || dmoMain.UserName == "")
    {
    MessageBox.Show("Please enter in valid connection details.",this.Text);
    }
    else
    {
    this.Cursor = Cursors.WaitCursor;
    statbarpnlMain.Text = "Connecting to SQL Server...";
    tvwServerExplorer.Nodes.Clear();
    try
    {
    dmoMain.Connect();
    Array aDatabases = (Array)dmoMain.Databases;
    dmoMain.DisConnect(); for (int i = 0; i < aDatabases.Length; i++)
    {
    TreeNode treenodeDatabase = new TreeNode(aDatabases.GetValue(i).ToString(), 0, 0);
    treenodeDatabase.Nodes.Add("");
    tvwServerExplorer.Nodes.Add(treenodeDatabase);
    } this.Cursor = Cursors.Default;
    statbarpnlMain.Text = "Connectiong successful, databases listed...";
    }
    catch
    {
    this.Cursor = Cursors.Default;
    statbarpnlMain.Text = "Connectiong un-successful...";
    MessageBox.Show("Connection to database failed. Please check your Server Name, User and Password.", this.Text);
    }
    }
    } private void txtPassword_Enter(object sender, System.EventArgs e)
    {
    if (txtPassword.Text == "Password") 
    {
    txtPassword.Text = "";
    char chPassword = '*';
    txtPassword.PasswordChar = chPassword;
    }
    } private void txtUser_Enter(object sender, System.EventArgs e)
    {
    if (txtUser.Text == "User") txtUser.Text = "";
    } private void tvwServerExplorer_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
    {
    if (e.Node.ImageIndex == 0)
    {
    this.Cursor = Cursors.WaitCursor;
    statbarpnlMain.Text = "Listing Tables...";
    dmoMain.Database = e.Node.Text;
    e.Node.Nodes.Clear();
    try
    {
    dmoMain.Connect();
    Array aTables = (Array)dmoMain.Tables;
    dmoMain.DisConnect();
    for (int i = 0; i < aTables.Length; i++)
    {
    TreeNode treenodeTable = new TreeNode(aTables.GetValue(i).ToString(), 1, 1);
    e.Node.Nodes.Add(treenodeTable);
    }

    this.Cursor = Cursors.Default;
    statbarpnlMain.Text = "Tables listed...";
    }
    catch
    {
    this.Cursor = Cursors.Default;
    statbarpnlMain.Text = "Problem listing Tables...";
    MessageBox.Show("Problem connecting to database. Cannot list tables, reconnect advised.", this.Text);
    }
    }
    } private void tvwServerExplorer_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
    {
    TreeNode tnodeSelected = (TreeNode)e.Node;

    if (tnodeSelected.ImageIndex == 2)
    {
    this.Cursor = Cursors.WaitCursor;
    statbarpnlMain.Text = "Generating Stored Procedure, please wait...";
    TreeNode tnodeTable = (TreeNode)tnodeSelected.Parent;
    dmoMain.Table = tnodeTable.Text;
    StoredProcedureTypes spType = new StoredProcedureTypes();
    dmoMain.Connect();
    dmoMain.DisConnect();
    this.Cursor = Cursors.Default;
    statbarpnlMain.Text = "Stored Procedure generated...";
         }
    } private void txtUser_Leave(object sender, System.EventArgs e)
    {
    if (txtUser.Text == "")
    txtUser.Text = "User";
    else
    dmoMain.UserName = txtUser.Text;
    } private void txtPassword_Leave(object sender, System.EventArgs e)
    {
    if (txtPassword.Text == "")
    {
    txtPassword.Text = "Password";
    char chResetPassword = (char)0;
    txtPassword.PasswordChar = chResetPassword;
    dmoMain.Password = "";
    }
    else
    dmoMain.Password = txtPassword.Text;
    } private void selServers_Leave(object sender, System.EventArgs e)
    {
    if (selServers.Text == "")
    selServers.Text = "Select server";
    else
    dmoMain.ServerName = selServers.Text;
    }

    }
    }
      

  9.   

    解决这个,需要3个文件,一个framset,一个tree,一个details
    http://www.coil.softhome.cn:8800/steel/steel_WebUI/tables.htm
    下面是tree
    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
    DataTable myTables = new DataTable();
    SqlDataAdapter mySQL = new SqlDataAdapter("get_Tables",myConnection);
    mySQL.SelectCommand.CommandType = CommandType.StoredProcedure;
    mySQL.Fill(myTables);
    for(int i=0;i<myTables.Rows.Count;i++)
    {
                 TreeNode myNode = new TreeNode();
    myNode.Text = myTables.Rows[i][2].ToString();
    myNode.NavigateUrl = "WebForm2.aspx?tableName="+myNode.Text;
    myNode.Target = "main";
    TreeView1.Nodes.Add(myNode);
    }
    下面是detail
    string tableName = Request.QueryString["tableName"];
    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
    DataTable myData = new DataTable();
    SqlDataAdapter mySQL = new SqlDataAdapter("select * from "+tableName,myConnection);
    mySQL.Fill(myData);
    DataGrid1.DataSource = myData;
    DataGrid1.DataBind();
      

  10.   

    如果用一个文件,就用楼2说的那样重新绑定,并在Session中添加被选位置,反正更麻烦。