Forum Moderators: open
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()
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