winform的treeview控件读取数据库生成节点,接下来该怎么做

winform的treeview控件读取数据库生成节点,接下来该怎么做,第1张

这个是商品类型分级的代码,给你参考一下,主要是递归调用节点的加载

private void RefreshTreeView() //根节点,所有节点挂在这个节点下

{

TreeNode tmp = new TreeNode("商品分类")//根节点

tmp.Tag = ""

this.GoodsTypetreeView.Nodes.Add(tmp)

//获取数据,显示到GoodsTypetreeView

String sqlStr = String.Format(@"Select TypeCode,TypeName,ParentTypeCode,case IsCommon when '0' then '否' else '是'

end as IsCommon,case Status when '0' then '未用' else '使用' end as Status,Remark from Com_GoodsType where Status = 1")

DataSet ds = SqlDBHelper.GetDs(sqlStr)

//nodes(this.GoodsTypetreeView.Nodes, ds, 0)

nodes(tmp, ds, "")

}

/// <summary>

/// 获得目标服务器所有数据库

/// </summary>

/// <param name="serverName"></param>

/// <param name="userName"></param>

/// <param name="password"></param>

public void getDataBaseNameList(string serverName, string userName, string password)

{

SQLDMO.Application sqlApplication = new SQLDMO.ApplicationClass()

SQLDMO.SQLServer sqlServer = new SQLDMO.SQLServerClass()

sqlServer.Connect(serverName, userName, password) // 连接服务器

foreach (SQLDMO.Database databBase in sqlServer.Databases)

{

if (databBase.Name != null)

{

this.DataBaseTreeView.Nodes.Add(databBase.Name)

getDataBaseTableList(serverName, userName, password, databBase.Name)

}

}

}

/// <summary>

/// 加载数据库中表

/// </summary>

/// <param name="serverName">服务器名</param>

/// <param name="userName">用户名</param>

/// <param name="password">密码</param>

/// <param name="dataBaseName">数据库名</param>

private void getDataBaseTableList(string serverName, string userName, string password, string dataBaseName)

{

SQLDMO.SQLServer Server = new SQLDMO.SQLServerClass()

//连接到服务器

Server.Connect(serverName, userName, password)

//对所有的数据库遍历,获得指定数据库

for (int i = 0i <Server.Databases.Counti++)

{

//判断当前数据库是否是指定数据库

if (Server.Databases.Item(i + 1, "dbo").Name == dataBaseName)

{

//获得指定数据库

SQLDMO._Database db = Server.Databases.Item(i + 1, "dbo")

//获得指定数据库中的所有表

for (int j = 0j <db.Tables.Countj++)

{

this.DataBaseTreeView.Nodes[i].Nodes.Add(db.Tables.Item(j + 1, "dbo").Name)

}

}

}

}

/// <summary>

/// 获得表中所有列名

/// </summary>

/// <param name="serverName">服务器名</param>

/// <param name="userName">用户名</param>

/// <param name="password">密码</param>

/// <param name="tableName">表名</param>

/// <param name="dataBaseName">数据库名</param>

/// <returns></returns>

public string getRowListFromTable(string serverName, string userName, string password, string tableName, string dataBaseName)

{

string result = string.Empty

string connectionString = string.Empty

connectionString += "server=" + serverName

connectionString += "Pwd=" + password

connectionString += "UID=" + userName

connectionString += "Database=" + dataBaseName

string commandString = string.Empty

commandString += "select name from syscolumns where id=object_id('"

commandString += tableName

commandString += "')"

SqlConnection sqlConnection = new SqlConnection(connectionString)

SqlCommand sqlCommand = new SqlCommand(commandString, sqlConnection)

SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, sqlConnection)

DataSet dataSet = new DataSet()

dataAdapter.Fill(dataSet)

DataTable dataTable = dataSet.Tables[0]

// DataTable dataTable = sqlConnection.GetSchema("Tables")

foreach (DataRow row in dataTable.Rows)

{

result += row[0].ToString() + "-"

}

if (result != null)

{

return result

}

else

{

return "0"

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6766410.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存