代码将VB.NET转换为C# [英] Code Convert VB.NET to C#

查看:72
本文介绍了代码将VB.NET转换为C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我在VB.NET中有几行代码,无法将其转换为c#.请任何人帮助我.提前谢谢.



Hi! I have a lines of codes in VB.NET and unable to convert it to c#. Please anyone help me. Thanks in advance.



Dim DSNWind As DataSet
       Dim CNnwind As New SqlClient.SqlConnection("DATA SOURCE=Server;USER ID=sa;INITIAL CATALOG=northwind;") '<==== CHANGE HERE
       Dim DACustomers As New SqlClient.SqlDataAdapter("SELECT CustomerID, CompanyName, ContactName, Country FROM customers WHERE country = 'Germany'", CNnwind)
       Dim DAOrders As New SqlClient.SqlDataAdapter("SELECT CustomerID, OrderID, OrderDate, ShippedDate, ShipVia, Freight FROM orders where customerid in (select customerid from customers where country = 'Germany')", CNnwind)
       Dim DAOrderDetails As New SqlClient.SqlDataAdapter("Select * from [Order Details] where OrderID in (SELECT OrderID FROM orders where customerid in (select customerid from customers where country = 'Germany'))", CNnwind)

       DSNWind = New DataSet()
       CNnwind.Open()
       DACustomers.Fill(DSNWind, "dtCustomers")
       DAOrders.Fill(DSNWind, "dtOrders")
       DAOrderDetails.Fill(DSNWind, "dtOrderDetails")
       'Close the connection to the data store; free up the resources
       CNnwind.Close()

       'Create a data relation object to facilitate the relationship between the Customers and Orders data tables.
       DSNWind.Relations.Add("CustToOrd", DSNWind.Tables("dtCustomers").Columns("CustomerID"), DSNWind.Tables("dtOrders").Columns("CustomerID"))
       DSNWind.Relations.Add("OrdToDet", DSNWind.Tables("dtOrders").Columns("OrderID"), DSNWind.Tables("dtOrderdetails").Columns("OrderID"))
       '''''''''''''''''''''''
       TreeView1.Nodes.Clear()
       //Dim i, n As Integer
       Dim parentrow As DataRow
       Dim ParentTable As DataTable
       ParentTable = DSNWind.Tables("dtCustomers")

       For Each parentrow In ParentTable.Rows
           Dim parentnode As TreeNode
           parentnode = New TreeNode(parentrow.Item(0))
           TreeView1.Nodes.Add(parentnode)
           ''''populate child'''''
           '''''''''''''''''''''''
           Dim childrow As DataRow
           Dim childnode As TreeNode
           childnode = New TreeNode()
           For Each childrow In parentrow.GetChildRows("CustToOrd")
               childnode = parentnode.Nodes.Add(childrow(0) & " " & childrow(1) & " " & childrow(2))
               childnode.Tag = childrow("OrderID")
               ''''populate child2''''
               ''''''''''''''''''''''''''
               Dim childrow2 As DataRow
               Dim childnode2 As TreeNode
               childnode2 = New TreeNode()
               For Each childrow2 In childrow.GetChildRows("OrdToDet")
                   childnode2 = childnode.Nodes.Add(childrow2(0))

               Next childrow2
               ''''''''''''''''''''''''

           Next childrow
           '''''''''''''''
       Next parentrow

推荐答案

开发人员融合 [ ^ ]自动翻译了它(一旦我在Nodes.Clear之后立即更正了评论)

Developer fusion[^] auto translated it (once I had corrected the comment immediately after the Nodes.Clear)

DataSet DSNWind = null;
System.Data.SqlClient.SqlConnection CNnwind = new System.Data.SqlClient.SqlConnection("DATA SOURCE=Server;USER ID=sa;INITIAL CATALOG=northwind;");
//<==== CHANGE HERE 
System.Data.SqlClient.SqlDataAdapter DACustomers = new System.Data.SqlClient.SqlDataAdapter("SELECT CustomerID, CompanyName, ContactName, Country FROM customers WHERE country = 'Germany'", CNnwind);
System.Data.SqlClient.SqlDataAdapter DAOrders = new System.Data.SqlClient.SqlDataAdapter("SELECT CustomerID, OrderID, OrderDate, ShippedDate, ShipVia, Freight FROM orders where customerid in (select customerid from customers where country = 'Germany')", CNnwind);
System.Data.SqlClient.SqlDataAdapter DAOrderDetails = new System.Data.SqlClient.SqlDataAdapter("Select * from [Order Details] where OrderID in (SELECT OrderID FROM orders where customerid in (select customerid from customers where country = 'Germany'))", CNnwind);

DSNWind = new DataSet();
CNnwind.Open();
DACustomers.Fill(DSNWind, "dtCustomers");
DAOrders.Fill(DSNWind, "dtOrders");
DAOrderDetails.Fill(DSNWind, "dtOrderDetails");
//Close the connection to the data store; free up the resources
CNnwind.Close();

//Create a data relation object to facilitate the relationship between the Customers and Orders data tables.
DSNWind.Relations.Add("CustToOrd", DSNWind.Tables["dtCustomers"].Columns["CustomerID"], DSNWind.Tables["dtOrders"].Columns["CustomerID"]);
DSNWind.Relations.Add("OrdToDet", DSNWind.Tables["dtOrders"].Columns["OrderID"], DSNWind.Tables["dtOrderdetails"].Columns["OrderID"]);
///''''''''''''''''''''
TreeView1.Nodes.Clear();
////Dim i, n As Integer
DataRow parentrow = null;
DataTable ParentTable = null;
ParentTable = DSNWind.Tables["dtCustomers"];
foreach (DataRow parentrow_loopVariable in ParentTable.Rows) {
	parentrow = parentrow_loopVariable;
	TreeNode parentnode = default(TreeNode);
	parentnode = new TreeNode(parentrow[0]);
	TreeView1.Nodes.Add(parentnode);
	///'populate child'''''
	///''''''''''''''''''''
	DataRow childrow = null;
	TreeNode childnode = default(TreeNode);
	childnode = new TreeNode();
	foreach (DataRow childrow_loopVariable in parentrow.GetChildRows("CustToOrd")) {
		childrow = childrow_loopVariable;
		childnode = parentnode.Nodes.Add(childrow[0] + " " + childrow[1] + " " + childrow[2]);
		childnode.Tag = childrow["OrderID"];
		///'populate child2''''
		///'''''''''''''''''''''''
		DataRow childrow2 = null;
		TreeNode childnode2 = default(TreeNode);
		childnode2 = new TreeNode();
		foreach (DataRow childrow2_loopVariable in childrow.GetChildRows("OrdToDet")) {
			childrow2 = childrow2_loopVariable;
			childnode2 = childnode.Nodes.Add(childrow2[0]);

		}
		///'''''''''''''''''''''

	}
	///''''''''''''
}


它显示一条错误消息无法从对象"转换为字符串".
It is showing an error message "Cannot convert from ''object'' to ''string''.


.NET代码转换-转换代码 [

这篇关于代码将VB.NET转换为C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆