C#。如何在记录集上执行查询?

时间:2009-01-23 10:58:29

标签: c#

HI,

我正在尝试将以下vba代码转换为c#,我面临一些错误。希望有人可以帮助我

vba代码

    Open "C:\testfile.txt" For Input As #1
        varii = ""
        Do While Not EOF(1)
        Line Input #1, strField
        varii = varii & "," & strField
        Loop
        Close #1
        astrFields = Split(varii, ",")

    For intIx = 1 To UBound(astrFields)
    counter = 0
    i = i + 1

    Dim cn As New ADODB.Connection, cn2 As New ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim connString As String
    Dim SelectFieldName
    Set cn = CurrentProject.Connection
    SelectFieldName = astrFields(intIx)
    Set rs = cn.OpenSchema(adSchemaColumns, _
    Array(Empty, Empty, Empty, SelectFieldName))

        If Left(rs!Table_Name, 4) <> "MSys" And Left(rs!Table_Name, 4) <> "Abfr
    " Then

strSql = "SELECT t.[" & astrFields(intIx) & "], t.fall from [" & rs!Tab
    le_Name & "] t Inner Join 01UMWELT on t.fall = [01UMWELT].fall "
            End If
    Set rs3 = CurrentDb.OpenRecordset(strSql)
    Do While Not rs3.EOF
    With rs3
    feedbackmsg = "Processing " & rs!Table_Name & " Record no : "
    & .Fields(1)
    SysCmd acSysCmdSetStatus, feedbackmsg
    varii = Nz(.Fields(astrFields(intIx)), "NullValue")
    If varii = "NullValue" Then
    Call .Edit
    .Fields(astrFields(intIx)) = 888
    Call .Update

这是我编码的c#代码

FileInfo theSourceFile = new FileInfo("C:\\csharp\\testfile.txt");
            StreamReader reader = theSourceFile.OpenText();
            varii = "";

            do
            {
                text = reader.ReadLine();
                varii = varii + "," + reader.ReadLine();
                //Console.WriteLine(text); 

            } while (text != null);



            string[] split = varii.Split(new Char[] {' '});

            foreach (string s in split)
            {

                if (s.Trim() != "")
                    Console.WriteLine(s);
            }


                    int temp = split.GetUpperBound(1);
                    for (intix = 1; intix <= temp; intix++)
                    {
                        counter = 0;

                        i++;
                        ADODB.Connection cn = new ADODB.Connection();
                        ADODB.Connection cn2 = new ADODB.Connection();
                        ADODB.Recordset rs;
                        object selectfieldname;

                        //ConnectionClass conDatabase = new ADODB.Connection();

                        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                              "Data Source='C:\\csharp\\accident_database.mdb';";

                        rs = cn.OpenSchema(ADODB.SchemaEnum.adSchemaTables, Missing.Value, Missing.Value);


                        if (Microsoft.Vbe.Left(rs.Fields("Table_Name").Value, 4) != "MSys" && +
                            Microsoft.Vbe.Left(rs.Fields("Table_Name").Value, 4) != "Abfr")
                        {
                            strsql = "SELECT t.[" + split(intix) + "],t.fall from [" + rs.Fields("Table_Name").Value + "]" +
                                    "t Inner join 01umwelt on t.fall = [01umwelt].fall";

                        }
                        rs3 = CurrentDB.OpenRecordset(strsql);

                        while (!rs3.EOF)
                        {

                            feedbackmsg = "Processing" + rs.Fields("Table_Name").Value + "Record no:" + rs3.Fields(1).Value;
                            SysCmd acSysCmdSetStatus, feecbackmsg;
                            varii = Nz(rs3.Fields(astrfields(intix)).Value, "NullValue");

                            if (varii == "NullValue")
                            {
                                rs3.Edit();
                                rs3.Fields[astrfields(intix)].Value = 888;
                                rs3.Update();

我收到了if语句中的错误

if (Microsoft.Vbe.Left(rs.Fields("Table_Name").Value, 4) != "MSys" && +
            Microsoft.Vbe.Left(rs.Fields("Table_Name").Value, 4) != "Abfr")

这里Vbe.left没有被接受

其次在我之后的sql语句中得到错误拆分是一个变量但是像方法一样使用。

由于

3 个答案:

答案 0 :(得分:0)

转换时请记住,VBA对索引和方法调用使用相同的operator(),而C#对索引使用[],对方法调用使用()。

split(intix)

应该是

split[intix]

尝试

rs.Fields("Table_Name").Value.ToString().Substring(0,4), but check the length first. 

ie

rs.Fields("Table_Name").Value.ToString().Length >= 4

答案 1 :(得分:0)

如果您在顶部添加using VB = Microsoft.VisualBasic;命名空间,则可以使用VB.Left()代替Microsoft.Vbe.Left()

答案 2 :(得分:0)

另一方面说明: 在for循环中创建和打开连接并不是一个好主意。这对性能不利。

您可以在for循环外创建并打开与DB的连接,在处理过程中使用相同的连接,并确保在完成工作后关闭连接。 就像你现在已经实现它一样,你的连接一直打开/关闭,在这种情况下完全没有必要。