谁能帮助我将这个VB代码转换为C#? [英] Can anyone help me convert this VB code to C#?

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

问题描述

您好我想问任何可以帮我将这个VB代码转换为C#的人。提前谢谢







Hi i would like to ask anybody who could help me convert this VB code to C#. thank you in advance



Private Sub updateLastLineItem()
    Dim rsLastItem As DAO.Recordset
    Dim rsPO As DAO.Recordset
    Set rsLastItem = CurrentDb.OpenRecordset("qry_PO_Networks_LastLineItem")
    Set rsPO = CurrentDb.OpenRecordset("tbl_PO_Infos")
    
    If Not rsLastItem.EOF Then
        rsLastItem.MoveFirst
        Do While Not (rsLastItem.EOF)
            rsPO.FindFirst "[PONumber] = '" & rsLastItem!PONumber & "'"
            If Not rsPO.NoMatch Then
                rsPO.Edit
                rsPO!LastLineItemSAP = rsLastItem![MaxOfLineItem]
                rsPO!LastLineItemCMA = rsLastItem![MaxOfLineItem]
                rsPO.Update
            End If
            rsLastItem.MoveNext
        Loop
    End If
    rsLastItem.Close
    Set rsLastItem = Nothing
    rsPO.Close
    Set rsPO = Nothing
End Sub





我的尝试:



i无法在do while部分下进行foreach工作。它也给了我关于DB连接的错误。还存储了值。我很简单,代码很容易理解它只是我不熟悉c#语法。检查_LastLineItem查询的数据然后如果没有匹配tbl_PO_Infos那么它将插入数据。



What I have tried:

i having trouble making the foreach work under the do While part. also it gives me error about the connection to the DB.also the storing of the values . i thin kthe code is easy to understand its just that im not really familiar with c# SYNTAX. it checkes t he data on _LastLineItem query then if there is no match against tbl_PO_Infos then it will insert data.

推荐答案

这是VBA代码 - C#不使用DAO对象它使用SqlConnection,SqlCommand,SqlDataAdapter及其ODBC等价物等。



没有直接将该代码转换为SQL:没有RecordSet。 NET



相反,您需要使用.NET类重写.NET。或者找到类似的基于.NET的代码。
That's VBA code - C# doesn't use DAO objects, it uses SqlConnection, SqlCommand, SqlDataAdapter, and their ODBC equivalents, among others.

There is no direct translation of that code to SQL: there is no RecordSet in .NET

Instead, you will need to rewrite it for .NET, using .NET classes. Or find .NET based code that does something similar.


这应该可以帮助你:



This should help you:

<pre>        private void updateLastLineItem1() {
            /*
            Private Sub updateLastLineItem()
                Dim rsLastItem As DAO.Recordset
                Dim rsPO As DAO.Recordset
                Set rsLastItem = CurrentDb.OpenRecordset("qry_PO_Networks_LastLineItem")
                Set rsPO = CurrentDb.OpenRecordset("tbl_PO_Infos")

                If Not rsLastItem.EOF Then
                    rsLastItem.MoveFirst
                    Do While Not (rsLastItem.EOF)
                        rsPO.FindFirst "[PONumber] = '" & rsLastItem!PONumber & "'"
                        If Not rsPO.NoMatch Then
                            rsPO.Edit
                            rsPO!LastLineItemSAP = rsLastItem![MaxOfLineItem]
                            rsPO!LastLineItemCMA = rsLastItem![MaxOfLineItem]
                            rsPO.Update
                        End If
                        rsLastItem.MoveNext
                    Loop
                End If
                rsLastItem.Close
                Set rsLastItem = Nothing
                rsPO.Close
                Set rsPO = Nothing
            End Sub
            */

            // connecting to the database
            //https://msdn.microsoft.com/en-us/library/ms254507(v=vs.100).aspx
            // using a DataAdapter or DataReader
            //https://msdn.microsoft.com/en-us/library/ms254931(v=vs.100).aspx
            //encrypting/decrypting connection strings in a web.config or app.config file
            //https://msdn.microsoft.com/en-us/library/89211k9b(VS.80).aspx

            //connection strings sometimes contain a backslash "\" character so you might need the "@" prefix to ensure the "\" is treated as a string literal instead of an escape character sequence (e.g. "\r" = carriage return in many c-like languages)
            System.String connectionString = @"server=localhost;integrated security=true;database=pubs";

            //NOTE: if you are not using a ms sql server database, try using System.Data.OleDb instead of System.Data.SqlClient

            // putting our c# code in a using statement ensures the connection is closed and resources are disposed with no memory leaks
            using (System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString))
            {
                //open the sql connection to the ms sql server database
                sqlConnection.Open();

                //declare a new SqlCommand object
                System.Data.SqlClient.SqlCommand qry_PO_Networks_LastLineItem;
                //instantiate a new SqlCommand object
                qry_PO_Networks_LastLineItem = new System.Data.SqlClient.SqlCommand();
                //set the Connection, CommandType and CommandText properties
                qry_PO_Networks_LastLineItem.Connection = sqlConnection;//our active "using" connection
                qry_PO_Networks_LastLineItem.CommandType = System.Data.CommandType.StoredProcedure;//change this when using a literal SQL string instead of a stored procedure
                qry_PO_Networks_LastLineItem.CommandText = "qry_PO_Networks_LastLineItem";//the name of the stored procedure or literal SQL string as applicable

                //declare and instantiate a new System.Data.SqlDataAdapter object in one line
                System.Data.SqlClient.SqlCommand tbl_PO_Infos = new System.Data.SqlClient.SqlCommand();
                //set the Connection, CommandType and CommandText properties
                tbl_PO_Infos.Connection = sqlConnection;//our active "using" connection
                tbl_PO_Infos.CommandType = System.Data.CommandType.StoredProcedure;//change this when using a literal SQL string instead of a stored procedure
                tbl_PO_Infos.CommandText = "tbl_PO_Infos";//the name of the stored procedure or literal SQL string as applicable

                //declare and instantiate a new System.Data.SqlDataAdapter object in one line
                System.Data.SqlClient.SqlDataAdapter rsLastItemSqlDataAdapter  = new System.Data.SqlClient.SqlDataAdapter(qry_PO_Networks_LastLineItem);
                
                //we could again declare and instantiate a new System.Data.SqlDataAdapter object in one line
                //System.Data.SqlDataAdapter rsPOSqlDataAdapter = new System.Data.SqlDataAdapter("tbl_PO_Infos", sqlConnection);
                //but for clarity we use two lines
                //declare a new SQLDataAdapter object
                System.Data.SqlClient.SqlDataAdapter rsPOSqlDataAdapter;
                //instantiate a new SQLDataAdapter object passing in the name of the stored procedure and sql connection object
                rsPOSqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter(tbl_PO_Infos);

                //an ADO Recordset object most closely resemembles an ADO.NET DataTable object
                //declare and instantiate a new System.Data.DataTable object
                System.Data.DataTable rsLastItem = new System.Data.DataTable();
                
                //we could again declare and instantiate a new System.Data.DataTable object
                //System.Data.DataTable rsPO = new System.Data.DataTable();
                //but again for clarity we use two line
                //declare a new DataTable object
                System.Data.DataTable rsPO;
                //instantiate a new DataTable object
                rsPO = new System.Data.DataTable();

                //fill the DataTable object named rsLastItem with the results from running the assigned stored procedure sql command "qry_PO_Networks_LastLineItem"
                rsLastItemSqlDataAdapter.Fill(rsLastItem);

                //fill the DataTable object named rsPO with the results from running the assigned stored procedure sql command "tbl_PO_Infos"
                rsPOSqlDataAdapter.Fill(rsPO);


                //we could use a foreach loop
                //foreach (System.Data.DataRow rsLastItemDataRow in rsLastItem.Rows){ foreach (System.Data.DataRow rsPODataRow in rsLastItem.Rows){} }

                //or we can use a traditional for loop or while loop
                System.Int32 numberOfRowsInRsLastItemDataTable = rsLastItem.Rows.Count;
                System.Int32 currentRowNumberInRsLastItemDataTable = 0;

                System.Int32 numberOfRowsInRsPODataTable = rsPO.Rows.Count;
                System.Int32 currentRowNumberInRsPODataTable = 0;

                while (currentRowNumberInRsLastItemDataTable < numberOfRowsInRsLastItemDataTable)
                {
                    while (currentRowNumberInRsPODataTable < numberOfRowsInRsPODataTable)
                    {
                        if (rsLastItem.Rows[currentRowNumberInRsLastItemDataTable]["PONumber"] != rsPO.Rows[currentRowNumberInRsPODataTable]["PONumber"]) {
                            rsPO.Rows[currentRowNumberInRsPODataTable]["LastLineItemSAP"] = rsLastItem.Rows[currentRowNumberInRsLastItemDataTable]["MaxOfLineItem"];
                            rsPO.Rows[currentRowNumberInRsPODataTable]["LastLineItemCMA"] = rsLastItem.Rows[currentRowNumberInRsLastItemDataTable]["MaxOfLineItem"];
                        }
                        //same thing as currentRowNumberInRsPODataTable = currentRowNumberInRsPODataTable + 1;
                        currentRowNumberInRsPODataTable += 1;
                    }
                    //same thing as currentRowNumberInRsLastItemDataTable +=1;
                    currentRowNumberInRsLastItemDataTable = currentRowNumberInRsLastItemDataTable + 1;
                }
                //utilize the automated update mechansism
                System.Data.SqlClient.SqlCommandBuilder sqlCommandBuilder = new System.Data.SqlClient.SqlCommandBuilder(rsPOSqlDataAdapter);
                rsPOSqlDataAdapter.AcceptChangesDuringUpdate = true;
                //update the database
                rsPOSqlDataAdapter.Update(rsPO);
            }
        }







好​​了!



恭敬地,



John

@Smart3DWeb




GOOD IS COMING!

Respectfully,

John
@Smart3DWeb


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

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