Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

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
0
djsoltan
Asked:
djsoltan
  • 2
1 Solution
 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now