Results 1 to 6 of 6

Thread: SQL server Login from Windows Forms app

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    5

    Angry SQL server Login from Windows Forms app

    hello!

    The configuration is as follows
    Remote SQLserver 2005 back-end
    Windows Forms Front end (Visual studio 2008)

    Is there a way to provide the username and password for the data connection from a Login form and not have it stored in a Connection string?

    Even better, is there a way to invoke the SQL login window each time the app starts????

    I need each user to be able to use his own (sqlserver) login everytime the application is started.

    just like it happened with my access front end.

    any information will be greatly appreciated!!

    thank You!

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: SQL server Login from Windows Forms app

    Welcome to the forums

    Why not create a Login form which takes the relevant inputs and concatenates it with the Connection string?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: SQL server Login from Windows Forms app

    If it's possible to use the inbuilt SQL Server then I'd say that you'd have to use SQL Server Management Objects (SMO), which means adding a reference in your project. If you do want to create your own dialogue, which is simple enough, then you can use a SqlConnectionStringBuilder to build a connection string from the components.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Fanatic Member Satal Keto's Avatar
    Join Date
    Dec 2005
    Location
    Me.Location
    Posts
    518

    Re: SQL server Login from Windows Forms app

    I'm not certain if there is anyway of getting the SQL login window to show but you should be able to have a login form that the user supplies their username and password which is then used to check that they can login.

    This is just a bit of example code that should hopefully show you how I would go about doing this
    Code:
        Private Const CON_STR As String = "server=servername;uid=[username];pwd=[password];Initial catalog=defaultDB"
    
        Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
            Dim constr As String = ""
            If txtUser.Text.Trim.Length > 0 And txtPass.Text.Trim.Length > 0 Then
                constr = CON_STR.Replace("[username]", txtUser.Text.Trim)
                constr = constr.Replace("[password]", txtPass.Text.Trim)
                If checkConStr(constr) Then
                    'The user can connect using that username and password
                Else
                    'The user cannot connect using that username and password
                End If
            Else
                MsgBox("You must supply both a username and password in order to continue")
            End If
        End Sub
    
        Private Function checkConStr(ByVal constr As String) As Boolean
            Dim conn As New SqlConnection(constr)
            Dim rtn As Boolean = True
    
            Try
                conn.Open()
                conn.Close()
            Catch ex As Exception
                rtn = False
            End Try
    
            Return rtn
        End Function
    As mentioned above this is just an example piece of code, you should have a better method of storing the base connection string rather than a constant but for this quick example that should be ample.

    Anyway I hope that this helps you

    Satal

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    5

    Re: SQL server Login from Windows Forms app

    Thank you all for the quick replies!

    omg the brackets in the connection string never crossed my mind!!
    [password]


    I will try this and if it works I will simply kill myself!!

    i will look into the smo solution (sounds complicated though..).

    Again thank you all, I will post the outcomes...

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    5

    Re: SQL server Login from Windows Forms app

    Unfortunately No cookie. The steps are:
    1. added the SQLserver datasource with the wizard
    2. did not include the password in the connection string
    3. created 2 forms, a login form and a form with datagrid view of a table from my dbase.
    4. included the appropriate(?) code on the click event of the login button.
    5. Hit F5
    6. Insert right user and pass and click login (which also opens the form2) I get no errors, no bugs... and no data connection.!

    Also when I input a wrong user/password I get no error code.

    The code is as follows


    Imports System.Data.SqlClient

    Public Class Form1
    Public Function SelectRows(ByVal dataSet As DataSet, ByVal connectionString As String, ByVal queryString As String) As DataSet

    Using connection As New SqlConnection(connectionString)

    Dim adapter As New SqlDataAdapter()

    adapter.SelectCommand = New SqlCommand(queryString, connection)

    adapter.Fill(dataSet)

    Return dataSet

    End Using

    End Function
    Private Const masterConnectionString As String = "server=GEROLYMATOS\STUDIO;uid=[user];pwd=[pword];Initial catalog=master"

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

    masterConnectionString.Replace("[user]", TextBox1.Text.Trim)
    masterConnectionString.Replace("[pword]", TextBox2.Text.Trim)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Form2.Show()

    End Sub

    Any Ideas?
    Last edited by jerrykeel; May 27th, 2010 at 03:36 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width