2

我一直在网站上查看有关字符串连接及其参数的信息。我已经实现了与此代码的连接,但我不知道如何将参数与用户输入连接起来。

这是我的代码:

Imports System
Imports System.Data
Imports System.Data.OracleClient

Module OracleConnect

   Sub Main()
      Dim connString As String = "server = o92; uid = java2s;password = password;"

      'Create connection
      Dim conn As New OracleConnection(connString)

      Try
         conn.Open()
         Console.WriteLine("Connection Opened")

         Console.WriteLine("Connection Properties")
         Console.WriteLine("- ConnectionString : {0}", conn.ConnectionString)
         Console.WriteLine("- ServerVersion : {0}", conn.ServerVersion)
         Console.WriteLine("- State : {0}", conn.State)

      Catch ex As OracleException
         Console.WriteLine("Error: " & ex.ToString())
      Finally
         ' Close Connection
         conn.Close()
         Console.WriteLine("Connection Closed")

      End Try

   End Sub

关键是我想用用户输入参数更改连接字符串,因此它将是一个文本框,服务器和密码以及侦听器将跟随用户的输入。

非常感谢

4

2 回答 2

0

你可以试试这样的函数,传入你的文本框的值;

Public Function connectionString(server As String, uid As String, password As String) As String
    Dim tmpServer(2) As String

     tmpServer(0) = "server = " & server
    tmpServer(1) = "uid = " & uid
    tmpServer(2) = "password = " & password

    Return String.Join("; ", tmpServer) & ";"
End Function

用法会。

Dim conn As New OracleConnection(connectionString(TextBox1.Text, TextBox2.Text, TextBox3.Text))
于 2012-06-30T06:16:57.497 回答
0

看起来您启动了一个控制台项目而不是一个 winform 项目。后者将允许您将文本框和按钮等控件拖放到表单上,以便用户能够填写连接字符串字段。

然后你可以像这样连接连接字符串:

Dim connString As String = "server = " & Textbox1.Text &"; uid = " & Textbox2.Text &";password = " & Textbox3.Text &";"

或者您可以使用控制台应用程序并使用main 方法中的 args 使用命令行参数启动它

于 2012-06-30T06:00:03.553 回答