回 帖 发 新 帖 刷新版面

主题:谁能教我下这个问题VB.NET+MSSQL2000

大家好,现在小弟有个问题想请你们帮下忙。
我的系统是VB.net+MSSQL2000开发,现在的问题是:
我用了个本地数据库连接进入系统,进去后,我又做了个窗体是连接到第二个数据库, 
主窗体的代码是:
Private Sub MenuItem70_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem70.Click
        Dim Inter As New FrmInter
        Inter.Show()
    End Sub
第二个窗体连接数据库代码是:
Public Class FrmInter
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents txtServer As Uc_TextBox
    Friend WithEvents TxtDatabase As Uc_TextBox
    Friend WithEvents TxtUser As Uc_TextBox
    Friend WithEvents TxtPwd As Uc_TextBox
    Friend WithEvents BtnOK As System.Windows.Forms.Button
    Friend WithEvents BtnExit As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(FrmLogon))
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.txtServer = New GdiApplication.Uc_TextBox(Me.components)
        Me.TxtDatabase = New GdiApplication.Uc_TextBox(Me.components)
        Me.TxtUser = New GdiApplication.Uc_TextBox(Me.components)
        Me.TxtPwd = New GdiApplication.Uc_TextBox(Me.components)
        Me.BtnOK = New System.Windows.Forms.Button
        Me.BtnExit = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Label3
        '
        Me.Label3.Font = New System.Drawing.Font("楷体_GB2312", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.Label3.Location = New System.Drawing.Point(48, 102)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(88, 24)
        Me.Label3.TabIndex = 9
        Me.Label3.Text = "登录名:"
        Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("楷体_GB2312", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.Label2.Location = New System.Drawing.Point(48, 70)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(88, 24)
        Me.Label2.TabIndex = 8
        Me.Label2.Text = "数据库:"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label4
        '
        Me.Label4.Font = New System.Drawing.Font("楷体_GB2312", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.Label4.Location = New System.Drawing.Point(48, 136)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(88, 24)
        Me.Label4.TabIndex = 10
        Me.Label4.Text = "密  码:"
        Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("楷体_GB2312", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.Label1.Location = New System.Drawing.Point(48, 36)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(88, 24)
        Me.Label1.TabIndex = 7
        Me.Label1.Text = "服务器:"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'txtServer
        '
        Me.txtServer.Font = New System.Drawing.Font("楷体_GB2312", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.txtServer.Location = New System.Drawing.Point(128, 32)
        Me.txtServer.Name = "txtServer"
        Me.txtServer.Size = New System.Drawing.Size(176, 29)
        Me.txtServer.TabIndex = 11
        Me.txtServer.Text = "(local)"
        '
        'TxtDatabase
        '
        Me.TxtDatabase.Font = New System.Drawing.Font("楷体_GB2312", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.TxtDatabase.Location = New System.Drawing.Point(128, 65)
        Me.TxtDatabase.Name = "TxtDatabase"
        Me.TxtDatabase.Size = New System.Drawing.Size(176, 29)
        Me.TxtDatabase.TabIndex = 12
        Me.TxtDatabase.Text = "GDIApp"
        '
        'TxtUser
        '
        Me.TxtUser.Font = New System.Drawing.Font("楷体_GB2312", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.TxtUser.Location = New System.Drawing.Point(128, 98)
        Me.TxtUser.Name = "TxtUser"
        Me.TxtUser.Size = New System.Drawing.Size(176, 29)
        Me.TxtUser.TabIndex = 13
        Me.TxtUser.Text = "sa"
        '
        'TxtPwd
        '
        Me.TxtPwd.Font = New System.Drawing.Font("楷体_GB2312", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.TxtPwd.Location = New System.Drawing.Point(128, 132)
        Me.TxtPwd.Name = "TxtPwd"
        Me.TxtPwd.PasswordChar = Microsoft.VisualBasic.ChrW(35)
        Me.TxtPwd.Size = New System.Drawing.Size(176, 29)
        Me.TxtPwd.TabIndex = 14
        Me.TxtPwd.Text = ""
        '
        'BtnOK
        '
        Me.BtnOK.Font = New System.Drawing.Font("楷体_GB2312", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.BtnOK.Location = New System.Drawing.Point(200, 176)
        Me.BtnOK.Name = "BtnOK"
        Me.BtnOK.Size = New System.Drawing.Size(75, 32)
        Me.BtnOK.TabIndex = 15
        Me.BtnOK.Text = "确定"
        '
        'BtnExit
        '
        Me.BtnExit.Font = New System.Drawing.Font("楷体_GB2312", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.BtnExit.Location = New System.Drawing.Point(280, 176)
        Me.BtnExit.Name = "BtnExit"
        Me.BtnExit.Size = New System.Drawing.Size(75, 32)
        Me.BtnExit.TabIndex = 16
        Me.BtnExit.Text = "取消"
        '
        'FrmLogon
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(378, 215)
        Me.Controls.Add(Me.BtnExit)
        Me.Controls.Add(Me.BtnOK)
        Me.Controls.Add(Me.TxtPwd)
        Me.Controls.Add(Me.TxtUser)
        Me.Controls.Add(Me.TxtDatabase)
        Me.Controls.Add(Me.txtServer)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "FrmLogon"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "数据库连接... ..."
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public gSql As New SqlClient.SqlConnection
    Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click

        Try
            gSql.ConnectionString = "user id=" + Me.TxtUser.Text + ";data source=" + Me.txtServer.Text + ";persist security info=True;initial catalog=" + Me.TxtDatabase.Text + ";password=" + Me.TxtPwd.Text + ""
            gSql.Open()
            Me.Close()
        Catch ex As Exception
            MsgBox("连接数据库出错,请仔细检查你的参数!", MsgBoxStyle.Information + MsgBoxStyle.OKOnly, gTitle)
            Me.txtServer.Focus()
        End Try
    End Sub

    Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
        Me.Close()
    End Sub
End Class

请问现在我想在把第二个窗体里面的数据库连接gSql传回到主窗体里面来使用该怎么写代码?我这样做能不能传,如果能传怎么传,请教我一下,如果各位看明白我的意思的话,我想请问有没有更简单的方法,有的话也可以教我下,谢谢

回复列表 (共1个回复)

沙发

应该可以的,不过没有试过:
在主窗体中定义个第二个窗体的变量,然后通过变量加相应的数据,
应该可以的.

我来回复

您尚未登录,请登录后再回复。点此登录或注册