Oracle字符串连接属性

时间:2012-06-30 03:35:28

标签: sql vb.net oracle

我一直在网站上查看String连接及其参数。我已经使用此代码实现了连接,但我不知道如何将参数与用户输入连接起来。

这是我的代码:

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

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

非常感谢

2 个答案:

答案 0 :(得分:0)

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

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

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

或者您可以使用控制台应用程序并使用args in the main method

使用命令行参数启动它

答案 1 :(得分:0)

您可以尝试这样的函数,传入TextBoxes的值;

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))