Hi all, I trying (for 5 hours :-() to connect to SQL server. I'm looking for a sample in order to make a select in a table. In fact, I've got a form. inside this one, a textbox for login & a textbox for password. I want to check if this couple exists in the
database. Someone has an issue ? Regards Séb
Hi What database are you using. To connect to SQL is incredibly easy, especially if you use the MS Data Access Blocks. In the code below, the value of g_sTcdbConn is a database connectionString - something along the lines of "Persist Security Info=False;Data
Source=Maximus;Initial Catalog=tcdb;User ID=yourUser;Password=yourPassword;" Eg Imports system.data Imports system.data.SQLClient
Dim oSQL As New SqlCommand
Dim oConn As New SqlConnection
OpenConn(oConn, g_sTcdbConn)
With oSQL
.CommandText = theSQL
.Connection = oConn
.CommandType = CommandType.Text
.CommandTimeout = 600
End With
Dim oDr As SqlDataReader
oDr = oSQL.ExecuteReader
To test if your username and password are working, sign in to QA with that username and pwd and see if you can successfully
connect. If not, like say you're using MSDE, then get the demo of SQL Server, install the client tools only and use Enterprise Manager to create/rename some accounts. hth Damian
I'm using SQL SERVER. but it's not the connection to the database which is a pb (The connection is good) It's the execute of a SELECT * from toto where login="+textbox1.Text Séb
Dim sql As New SqlCommand( "SELECT * FROM Users WHERE Username=@Username", myConn )
myConn.Open()
Dim reader As SqlDataReader = sql.ExecuteReader()
...
myConn.Close()
In the ... section you can put in code to read against the results returned. For instance:
While (reader.Read())
Dim name As String = reader(0)
End While
This gets the first row (index 0) of the current row pointed to by the reader. The while loop ensures it executes for each row returned by the DataReader.
kristouf
Member
295 Points
59 Posts
ASP.NET & select
Jan 20, 2004 08:19 AM|LINK
Mr Pike
Member
665 Points
133 Posts
ASPInsiders
Re: ASP.NET & select
Jan 20, 2004 08:42 AM|LINK
Dim oSQL As New SqlCommand Dim oConn As New SqlConnection OpenConn(oConn, g_sTcdbConn) With oSQL .CommandText = theSQL .Connection = oConn .CommandType = CommandType.Text .CommandTimeout = 600 End With Dim oDr As SqlDataReader oDr = oSQL.ExecuteReaderTo test if your username and password are working, sign in to QA with that username and pwd and see if you can successfully connect. If not, like say you're using MSDE, then get the demo of SQL Server, install the client tools only and use Enterprise Manager to create/rename some accounts. hth Damiankristouf
Member
295 Points
59 Posts
Re: ASP.NET & select
Jan 20, 2004 09:00 AM|LINK
Stanley Tan
Contributor
2035 Points
405 Posts
Re: ASP.NET & select
Jan 20, 2004 10:08 AM|LINK
theSpoke Blog
kristouf
Member
295 Points
59 Posts
Re: ASP.NET & select
Jan 20, 2004 10:12 AM|LINK
Stanley Tan
Contributor
2035 Points
405 Posts
Re: ASP.NET & select
Jan 20, 2004 10:31 AM|LINK
While (reader.Read()) Dim name As String = reader(0) End WhileThis gets the first row (index 0) of the current row pointed to by the reader. The while loop ensures it executes for each row returned by the DataReader.theSpoke Blog
kristouf
Member
295 Points
59 Posts
Re: ASP.NET & select
Jan 20, 2004 10:34 AM|LINK