Monday, 19 March 2012

Connect SQL Server

I have troubles connecting to database now. Anyone please help me!

I connect to SQL Server but I'm not sure I'm doing the right way or not...
I have a table named "t_user" in SQL Server and the fields are
usrid varchar 5
usrpwd varbinary 256

Here is my code :
=================
Dimmyconn As SqlConnection
Dimmycmd As SqlCommand
Dimreader As SqlDataReader
DimstrUsr, strPwd As String

strUsr = txtUsr.Text

myconn = New SqlConnection("server=(local); database=test; user id=myuser; password=mypassword")
mycmd = myconn.CreateCommand()
mycmd.CommandText = "SELECT * FROM t_user WHERE usrid=@.username AND pwdcompare(UPPER(@.password) , usrpwd)=1 "

mycmd.Parameters.Add("@.username", SqlDbType.VarChar, 5).Value = txtUsr.Text
mycmd.Parameters.Add("@.password", SqlDbType.VarChar, 50).Value = txtPwd.Text

myconn.Open()
reader = mycmd.ExecuteReader()

If Not reader.Read() Then
lblMessage.Text = "<font color=red>Invalid username/password (" & strUsr & ")</font>"
Else
Session("invsystem_usr") = reader.GetString(0)
Response.Redirect("../main/main.aspx")

End If

reader.Close()
myconn.Close()
=================Hi,
if i recall clearly it's supposed to be in this format
myconn = New SqlConnection("server=localhost;database=mydatabasename;user id=myuser;password=mypassword")

note that there is no spaces in the string
btw database name is not the name of your table but the name of the database your table in in.
example Northwind|||Thank you :)|||server=(local) works fine. localhost is refered mainly by webserver in my mind so it always looks weird to see it in a connection string. :)

No comments:

Post a Comment