【问题标题】:How to connect using odbc to as400 for vb.net如何使用 odbc 连接到 as400 for vb.net
【发布时间】:2021-08-09 12:14:22
【问题描述】:

我正在尝试通过 vb.net 应用程序使用 ODBC 驱动程序连接到 AS400 服务器,但问题是我正在尝试填充数据集,并且每当我想显示数据时,我都找不到任何东西

这是我的代码:

    Dim cn As OdbcConnection
    Dim cm As OdbcCommand
    Dim dm As OdbcDataAdapter


    Sub ConnServer()
        Try
            cn = New OdbcConnection("DSN=AS400_CA;UID=root;PWD=*****;")
            cn.Open()

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Ecriture Comptable")
        End Try

    End Sub

    Public Function GetData(query As String) As DataTable
        Try

            cn = New OdbcConnection("DSN=AS400_CA;UID=root;PWD=*****;")
            Dim cmd As OdbcCommand = New OdbcCommand(query, cn)

            cn.Open()

            Dim ds = New DataSet()
            cmd.Connection = cn
            dm.SelectCommand = cmd
            dm.Fill(ds, "table")
            Dim data = ds.Tables("table")
            cn.Close()
            Return data
        Catch ex As Exception
            con.Close()
            Return New DataTable()
        End Try
    End Function
```

【问题讨论】:

  • 您是否检查了 GetData 是否以异常结尾?此代码无法告诉您是否有错误或查询是否生成空表
  • 你至少在GetData的Catch块中放了一个断点吗?同样在你的捕获中,你有 con.Close()。 con 来自哪里,其他地方都认为是 cn。您的代码也可能受益于“使用”语句,这将帮助您管理连接

标签: vb.net odbc ibm-midrange


【解决方案1】:

我不知道您使用的是哪个提供商。检查https://www.connectionstrings.com/as-400/ 以检查您的连接字符串。我没有看到与您的字符串语法匹配的连接字符串。

我不知道在 AS400 中选择字符串,所以我只使用了标准的 Sql 字符串。将您的 try/catch 放入 UI 代码中,这样您就可以显示带有错误的消息框。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dt As DataTable
    Try
        dt = GetData("Select * From SomeTable;")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
        Exit Sub
    End Try
    DataGridView1.DataSource = dt
End Sub

在你的函数中你返回一个DataTable,那么你为什么要搞乱DataSet?只需加载DataTableConnections、Commands 和 DataReaders 都需要处理。 Using...End Using 块处理关闭和处置,即使有错误。

Public Function GetData(query As String) As DataTable
    Dim dt As New DataTable
    Using cn = New OdbcConnection("DSN=AS400_CA;UID=root;PWD=*****;"),
        cmd As OdbcCommand = New OdbcCommand(query, cn)
        cn.Open()
        Using reader = cmd.ExecuteReader
            dt.Load(reader)
        End Using
    End Using
    Return dt
End Function

【讨论】:

    【解决方案2】:

    这个 C# 代码对我有用。

    我正在使用 IBM 访问客户端解决方案 64 位 ODBC 驱动程序。

    using System;
    using System.Data.Odbc;
    using System.Data;
    
    namespace IntroUI
    {
    class Program
    {
      static void Main(string[] args)
      {
    
    // make sure using 64 bit IBM i Access ODBC driver
      var conn = OpenConnection("dsn", "user", "pass");
    
      OdbcCommand cmd = conn.CreateCommand();
      var query = "select  a.* from qrpglesrc a";
      var table = GetData( conn, query ) ;
    
      var numRows = table.Rows.Count ;
      for( var ix = 0 ; ix < numRows ; ++ix )
      {
        var row = table.Rows[ix] ;
        var srcdta = row.ItemArray[2] ;
        Console.WriteLine( srcdta ) ;
        if ( ix > 20 )
          break ;
      }
    
      conn.Close( ) ;
    
      }
    
      // InDsn is the dsn from the odbc administration window
      static OdbcConnection OpenConnection( string InDsn, string InUser, string InPwd )
        {
          string connString = "DSN=" + InDsn + "; UID=" + InUser +
            "; PWD=" + InPwd + ";" ;
          OdbcConnection conn = new OdbcConnection( connString ) ;
          conn.Open( ) ;
          return conn ;
        }
    
        static DataTable GetData( OdbcConnection conn, string query )
        {
          OdbcDataAdapter dm = new OdbcDataAdapter() ;
          OdbcCommand cmd = new OdbcCommand(query, conn);
          DataSet ds = new DataSet( ) ;
          dm.SelectCommand = cmd ;
          dm.Fill( ds, "table") ;
          var data = ds.Tables["table"] ;
          return data ;
        }
    
      }
    }
    
    
    

    【讨论】:

    • 很多数据库对象,包括Connections和Commands都需要处理掉。在使用它的方法之外声明连接是不切实际的。
    猜你喜欢
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多