473,382 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

ADO.NET Connection to SQL Server Code

Here is what I have:

Dim strConn As String
Dim sqlCon As SqlClient.SqlConnection
Dim sqlCmd As SqlClient.SqlCommand
Dim sqlAdapt As SqlClient.SqlDataAdapter = New sqlclient.dataAdapter()
strConn = "data source=LHPSERVE;initial catalog=AssetTrakker;integrated
security=SSPI;persist security info=False;workstation id=LHPSERVE;packet
size=4096"

sqlCon = New SqlClient.SqlConnection(strConn)

sqlCon.Open()

dsAssets = New Data.DataSet("dsAssets")

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

sqlCmd.CommandType = CommandType.Text

sqlCmd.Connection = sqlCon

sqlAdapt.SelectCommand = sqlCmd

sqlAdapt.Fill(dsAssets, "tblAssets")

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

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

Thanks

Adam
Nov 20 '05 #1
2 1095
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", connectionObject)
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!
https://www.windowsembeddedeval.com/...ity/newsgroups
"Adam Clark" <ad********@lhpsoftware.com> wrote in message
news:e5**************@TK2MSFTNGP10.phx.gbl...
Here is what I have:

Dim strConn As String
Dim sqlCon As SqlClient.SqlConnection
Dim sqlCmd As SqlClient.SqlCommand
Dim sqlAdapt As SqlClient.SqlDataAdapter = New sqlclient.dataAdapter()
strConn = "data source=LHPSERVE;initial catalog=AssetTrakker;integrated
security=SSPI;persist security info=False;workstation id=LHPSERVE;packet
size=4096"

sqlCon = New SqlClient.SqlConnection(strConn)

sqlCon.Open()

dsAssets = New Data.DataSet("dsAssets")

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

sqlCmd.CommandType = CommandType.Text

sqlCmd.Connection = sqlCon

sqlAdapt.SelectCommand = sqlCmd

sqlAdapt.Fill(dsAssets, "tblAssets")

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

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

Thanks

Adam

Nov 20 '05 #2
Hi Adam,

I have nothing to add to what Bill wrote only he did not answer your
question completly in my idea :-)
sqlCmd = New SqlClient.SqlCommand("Select * from tblAssets")
sqlCmd.CommandType = CommandType.Text
sqlCmd.Connection = sqlCon
sqlAdapt.SelectCommand = sqlCmd


sqlAdapt = new sqlClient.SqlDataAdapter("Select * 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
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Fortepianissimo | last post by:
Here is the situation: I want my server started up upon connection. When the first connection comes in, the server is not running. The client realizes the fact, and then starts up the server and...
6
by: Paul Robinson | last post by:
I am developing a website in ASP that connects to a Sybase database. However, when I try to open a connection to the database the page will not load. The script does not timeout, nor the...
17
by: AMC | last post by:
Hi, I'm using an include file to store the connection string to a database. Whenever I try to reference that string to open a connection in the page that includes the file I get the error 'empy...
5
by: Fred Zuckerman | last post by:
Hello All, After reading in this group about the preference for connecting to a SQL Server using a connection string instead of a DSN file, I have done just that. BUT, I cannot update my data....
3
by: Martin B | last post by:
Hallo! I'm working with C# .NET 2.0, implementing Client/Server Applications which are connecting via Network to SQL-Server or Oracle Databases. To stay independent from the underlaying Database...
5
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
16
by: crbd98 | last post by:
Hello All, Some time ago, I implemented a data access layer that included a simple connectin pool. At the time, I did it all by myself: I created N connections, each connection associated with...
20
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I...
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
10
by: Elaine121 | last post by:
Hi i've been batteling for hours and can't seem to find the problem. When my server runs and I press the connect button the gui freezes until the client gui is terminated.. only then the gui becomes...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.