ADO.NET Connection to SQL Server Code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Clark

    ADO.NET Connection to SQL Server Code

    Here is what I have:

    Dim strConn As String
    Dim sqlCon As SqlClient.SqlCo nnection
    Dim sqlCmd As SqlClient.SqlCo mmand
    Dim sqlAdapt As SqlClient.SqlDa taAdapter = New sqlclient.dataA dapter()
    strConn = "data source=LHPSERVE ;initial catalog=AssetTr akker;integrate d
    security=SSPI;p ersist security info=False;work station id=LHPSERVE;pac ket
    size=4096"

    sqlCon = New SqlClient.SqlCo nnection(strCon n)

    sqlCon.Open()

    dsAssets = New Data.DataSet("d sAssets")

    sqlCmd = New SqlClient.SqlCo mmand("Select * from tblAssets")

    sqlCmd.CommandT ype = CommandType.Tex t

    sqlCmd.Connecti on = sqlCon

    sqlAdapt.Select Command = sqlCmd

    sqlAdapt.Fill(d sAssets, "tblAssets" )





    --------------------------------------------------------



    Is there a way to make this simpler? DO I really need the sqlCmd ?



    Thanks



    Adam


  • William Ryan  eMVP

    #2
    Re: ADO.NET Connection to SQL Server Code

    Technically Yes but practically no. The overload for the adapter taeks a
    command object but you can just pass in the sql string and specify a
    connection object and you're good to go.

    Also, don't open the connection yourself unless you have a good reason to
    and if you do, use a try/catch/finally and make SURE you close it in the
    finally..(Trust me on this!) You con't need to specify the CommandText if
    you do use a command and you can overload it so you save a line of code

    cmd = new SqlCommand("SQL STatemetn", connectionObjec t)
    By taking advantage of a few of the overloads you can cut down about 4 lines
    of code and if you pass in the SQL Statement, it will still create a
    SqlCommand behind the scenes but you don't have to.

    --
    W.G. Ryan MVP Windows - Embedded

    Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
    Let Microsoft know!

    "Adam Clark" <adam.clark@lhp software.com> wrote in message
    news:e5VYv$CdEH A.4092@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Here is what I have:
    >
    > Dim strConn As String
    > Dim sqlCon As SqlClient.SqlCo nnection
    > Dim sqlCmd As SqlClient.SqlCo mmand
    > Dim sqlAdapt As SqlClient.SqlDa taAdapter = New sqlclient.dataA dapter()
    > strConn = "data source=LHPSERVE ;initial catalog=AssetTr akker;integrate d
    > security=SSPI;p ersist security info=False;work station id=LHPSERVE;pac ket
    > size=4096"
    >
    > sqlCon = New SqlClient.SqlCo nnection(strCon n)
    >
    > sqlCon.Open()
    >
    > dsAssets = New Data.DataSet("d sAssets")
    >
    > sqlCmd = New SqlClient.SqlCo mmand("Select * from tblAssets")
    >
    > sqlCmd.CommandT ype = CommandType.Tex t
    >
    > sqlCmd.Connecti on = sqlCon
    >
    > sqlAdapt.Select Command = sqlCmd
    >
    > sqlAdapt.Fill(d sAssets, "tblAssets" )
    >
    >
    >
    >
    >
    > --------------------------------------------------------
    >
    >
    >
    > Is there a way to make this simpler? DO I really need the sqlCmd ?
    >
    >
    >
    > Thanks
    >
    >
    >
    > Adam
    >
    >[/color]


    Comment

    • Cor Ligthert

      #3
      Re: ADO.NET Connection to SQL Server Code

      Hi Adam,

      I have nothing to add to what Bill wrote only he did not answer your
      question completly in my idea :-)
      [color=blue]
      > sqlCmd = New SqlClient.SqlCo mmand("Select * from tblAssets")
      > sqlCmd.CommandT ype = CommandType.Tex t
      > sqlCmd.Connecti on = sqlCon
      > sqlAdapt.Select Command = sqlCmd[/color]

      sqlAdapt = new sqlClient.SqlDa taAdapter("Sele ct * from tblAssets", sqlCon)
      And you can skip all what is above.

      I think that this is the overloaded version Bill was talking about.

      I hope this helps?

      Cor


      Comment

      Working...