Visual Studio Connection String to MS SQL

hi guys,
i am totaly new with visual studio 2005 and i am working on a new project to be able to query data from a MS SQL server.
i've used php and mysql alot and im pretty familiar with it but with this visual basic im a little lost.
someone told me the connection string has to be saved in 'web.config' and someone told me it should be saved under master file.
anyways, could you please tell me how i should do it and provide me with a copy/paste code to use to get this runing?
thank you very much
djsoltanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

carmodykCommented:
Hit up the website http://www.connectionstrings.com It's been a good resource, plenty of different type of connection strings.  I'm sure that it will help with the connection.  

Here are some functions to help you with your running SQL queries for within your code:  

You must place in Imports System.Data.SQLClient at the top of your form code:

Private Sub AddYourInfo()

        Dim cnSQL As SQLConnection
        Dim cmSQL As SQLCommand
        Dim strSQL As String


        Try
            strSQL = "INSERT INTO YourTable (YourValue) " & _
                       "VALUES (" & PrepareStr(strInfo) & ")"

            cnSQL = New SQLConnection(m_YourConnectionString)
            cnSQL.Open()

            cmSQL = New SQLCommand(strSQL, cnSQL)
            cmSQL.ExecuteNonQuery()

        Catch e As SQLException
            MsgBox(e.Message, MsgBoxStyle.Critical, "Export Procedures SQL Error")

        Catch e As Exception
            MsgBox(e.Message, MsgBoxStyle.Critical, "Export Procedures General Error")

        Finally
            ' Close and Clean up objects
            cnSQL.Close()
            cmSQL.Dispose()
            cnSQL.Dispose()
        End Try

    End Sub

    Private Sub UpdateYourInfo(ByVal intYourID as integer)

        Dim cnSQL As SQLConnection
        Dim cmSQL As SQLCommand
        Dim strSQL As String

        Try
            strSQL = "UPDATE yourTable SET " & _
                      "YourColumn = " & PrepareStr(YourValue) &  " " & _
                      "WHERE (ID = " & intYourID & ")"

            cnSQL = New SQLConnection(YourConnectionString)
            cnSQL.Open()

            cmSQL = New SQLCommand(strSQL, cnSQL)
            cmSQL.ExecuteNonQuery()

        Catch Exp As SQLException
            MessageBox.Show(Exp.Message, "SQL Error Update")

        Catch Exp As Exception
            MessageBox.Show(Exp.Message, "Update General Error")
        Finally
            ' Close and Clean up objects
            cnSQL.Close()
            cmSQL.Dispose()
            cnSQL.Dispose()
        End Try

    End Sub

Public Function PrepareStr(ByVal strValue As String) As String

        strValue = strValue.Replace("'", "''")

        If (strValue.Trim() = "") Or (strValue Is Nothing) Then
            Return "NULL"
        Else
            Return "'" & strValue.Trim() & "'"
        End If
    End Function

I hope this helps you.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
carmodykCommented:
Oh, and regards to your connetion string being stored, you don't necessary have to have it stored in the webconfig area.   You can just declare it as a public string in your code or a general Private string for your VB forms near the top of your code, right after the Inherits System.Windows.Forms.Form declaration

Inherits System.Windows.Forms.Form
     Private strMyConnectionstring As string = "Data Source=yourDatabase;Initial Catalog=pubs;User Id=sa;Password=asdasd;"


   
0
djsoltanAuthor Commented:
thanks alot,
it sure helped alot :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.