Forum Moderators: open

Message Too Old, No Replies

SQL Express and vb win apps

cant connect

         

ebby

5:08 pm on Jun 17, 2008 (gmt 0)

10+ Year Member



Hi guys, I'm a real nub when it comes to developing win apps but I've been developing web apps with SQL Server and PHP for a while.

I have a desktop app that I want to embed an SQL Express database in. I can't for the life of me connect and insert data. Can you guys see a problem here?


Dim SQLConnectionString As String
Dim SQLConnection As New SqlClient.SqlConnection()
SQLConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=¦DataDirectory¦\MyDB.mdf;Integrated Security=True;User Instance=True;
SQLConnection.ConnectionString = SQLConnectionString
Dim myCommandText As String
myCommandText = "INSERT INTO MyTable(name, address, city, State, zip) Values(@name, @address, @city, @state, @zip)"
Dim myCommand As New SqlClient.SqlCommand(myCommandText, SQLConnection)
myCommand.Parameters.Add("name", SqlDbType.VarChar)
myCommand.Parameters("name").Value = TextBox1.Text
myCommand.Parameters.Add("address", SqlDbType.VarChar)
myCommand.Parameters("address").Value = TextBox2.Text
myCommand.Parameters.Add("city", SqlDbType.VarChar)
myCommand.Parameters("city").Value = TextBox3.Text
myCommand.Parameters.Add("state", SqlDbType.VarChar)
myCommand.Parameters("state").Value = TextBox4.Text
myCommand.Parameters.Add("zip", SqlDbType.Int)
myCommand.Parameters("zip").Value = TextBox5.Text
SQLConnection.Open()
myCommand.Connection = SQLConnection
myCommand.ExecuteNonQuery()
SQLConnection.Close()

Any help would be greatly appreciated

ebby

8:41 pm on Jun 19, 2008 (gmt 0)

10+ Year Member



So I can open and run the query, show it on the detailsview but when I stop debugging it's not written to the DB. Any thoughts?

Demaestro

9:47 pm on Jun 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Geez man haven't done VB since back in school which was forever ago.... but if memory serves I think you have to do something with an ODBC driver to make the connection.

Or maybe not have to but Microsoft plays nice with Microsoft and ODBC is the MS way of connecting to a DB.

You can go to the Admin Tools in Windows and open up ODBC from there it will give you a tool to connect... if you can connect with that it will have a connection string you can put in your app and it will connect through ODBC...

Good luck

Demaestro

9:56 pm on Jun 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh also maybe try a semi-colon after your query...

myCommandText = "INSERT INTO MyTable(name, address, city, State, zip) Values(@name, @address, @city, @state, @zip);"

I know I have to do that in PostGres

ebby

3:28 pm on Jun 20, 2008 (gmt 0)

10+ Year Member



Thanks for the replies. I'll try the semi-colon.

Demaestro

5:51 pm on Jun 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Were you able to connect?

I some this in code today and thought of this thread.

Maybe you need a COMMIT statement. If it is transactional that is most likely needed.

"INSERT INTO MyTable(name, address, city, State, zip) Values(@name, @address, @city, @state, @zip); COMMMIT;"