I want my vb.net program with sqldb to be transferable - vb.net

I'm having trouble with my vb.net program. It has a database with a SqlConnection string of:
DbConn = New SqlConnection("Data Source=ACE-DUO;Initial Catalog=db_CVSO;Persist Security Info=True;User ID=sa;Password=pwd")
I made an installer for this vb.net program but I'm having problems regarding my SQL Server connection string. It's because once I installed the program in different computer. The server name in my case (ACE-DUO) changes and the database itself cannot be located.
I know how to detach the file and attach it to vb.net program. what I'm really aiming for is that I want the connection string to change on based where the program resources were placed.
For example, if the program was installed in the C:\Program Files\MyDatabase folder, I want to make it as a part of the connection string so it would be opened in different computer.

If you don't need multiple shared access to your database you could take advandage of the LocalDB feature of Sql Server 2012.
You connection string could be changed to
DbConn = New SqlConnection("Server=(localdb)\v11.0;Integrated " & _
"Security=true;AttachDbFileName=C:\Program Files\MyDatabase\db.mdf;"
Article on LocalDB

Related

VB.net - Get error "Attempted to read or write protected memory..." since provider changed

That's my first post here, so please indicate me if there are some missing useful informations' to help to solve my problem.
Here is it:
I have a VB.net application using Access database (.mdb), which worked properly for years.
Because I'm using now Access Database Engine 2016 in 64-bits, I have changed my DB connection provider from Microsoft.Jet.OLEDB.4.0 (only available in 32-bits) to Microsoft.ACE.OLEDB.12.0. I've also unchecked in my app's properties "32-bits prefered" (translation from my French version of Visual Studio).
Since I did these changes (and only them), I often get the following error:
System.AccessViolationException: 'Attempted to read or write
protected memory. This is often an indication that other memory is
corrupt.
It appears (randomly) in different parts of the software, always at the line myConnection.Open()
Dim myConnection As OleDbConnection = New OleDbConnection(S7ConnString)
Try
myConnection.Open()
...
First new connection string with which I began to get this problem is:
Public S7ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBPath & ";Jet OLEDB:Database Password=***;"
(where DBPath is a string for the Access .mdb file path)
After some searches, I've read that adding OLE DB Services=-1 could solve it. It improved speed of DB processes really much, but did not solved the problem.
Would you have any idea to solve this?
Thank you & Kind regards.
I have uninstalled Access Database Engine 2016 and installed 2010 64-bits version, and it seems to work correctly now.
Thank you for your suggestions, #Jimi, that helped me to solve this. I did not imagine "Microsoft.ACE.OLEDB.12.0" provider was not compatible with MS Access Database Engine 2016!

MS Access & VB.Net Connection String in Settings Using User Profile

Hello I Have Made an Application with vb.net and ms access The Connection String in My.Settings is As Shown:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Ammar\AppData\Local\Empolyee Attendance\Data.mdb;Jet OLEDB:Database Password=ammar87
So as I have to install this application in many computers, I have tried to make my connection string as shown:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\%UserProfile%\AppData\Local\Empolyee Attendance\Data.mdb;Jet OLEDB:Database Password=ammar87
But It Did not work so how can I make the userprofile location in my connection string so that It can varies from computer to an other computer
Best Regards.

Setting Up SqlConnection string in vb.net for a local database created through visual studio 2010

I have been searching for a couple hours, and found several questions, but none of them really explained this in a way I can understand.
I'm programming a game similar to Rock Paper Sissors, except with many more selections, and the possiblity of a tie. I had originally hardcoded all of the possible outcomes, then decided to try a database so I can learn and practice sql as well.
Problem is, I can't figure out for the life of me how to connect to my local database, now that I have set it up and filled it through Visual Studio 2010. I can connect to it through Server Explorer just fine, and I can see it in Solution Explorer. I've tried several things, along the lines of:
Private sqlConn As New SqlConnection("Data Source=(local)|DataDirectory|\Outcomes.sdf;database=Outcomes;Integrated Security=true")
Private sqlConn As New SqlConnection("Data Source=.\SQLEXPRESS; Integrated Security=true")
Dim sqlConn As SqlConnection
sqlConn = New SqlConnection("DataSource=..\..\Outcomes.sdf")
I am relatively new to sql, but know enough through tinkering to build a sql statement and get me the result I want. But I've never connected to a database before. I've looked on MSDN and tried several things I saw on there (everything that looked like what I needed, really) but it still hasn't worked.
If I can connect, I already have my statement set, and have tested it through the database itself. Any help would be wonderful, especially if it's explained in a way I can understand it and use it for later.
Also, if it helps and isn't noticed through my tried code, my db name is Outcomes. I don't know if that is needed or will help, but just in case.
Please visit connection strings here...
It also would have been helpful to know what type of DBMS you are using as well. I noticed you have an .sdf database file, not a DBMS (For ex: MySql, SQL or Oracle). Therefore, your connection string is really going to depend on what you want.
Your issue is here by the way...
Private sqlConn As New SqlConnection("Data Source=(local)|DataDirectory|\Outcomes.sdf;database=Outcomes;Integrated Security=true")
*You cant use the SqlConnection you have because its not supported with the use of .sdf files.
Instead you have to use: System.Data.SqlServerCe 'This is for compact edition
If you would like to know more about this please see here.
Kendra,
Here are the logical Steps you will need to follow to access the database programmatically:
Note: I'm assumming you have the proper SQLExpress | SQL Server Database setup whether local or remote the methods below are identical except for the connection string information.
1) Import the Sql AdoNet Namespace so you can use the proper SQL Server Client Objects & Methods;
a) Imports System.Data.SqlClient
2) Establish a Connection to the database with the ADO Connection Object:
' Create your ADO Connection Object:
Private myConn As SqlConnection
myConn = New SqlConnection("Initial Catalog=OutComes;" & _
"Data Source=localhost;Integrated Security=SSPI;")
Note: This connection string uses integrated security from your windows machine. you could also use standard security where you would need to enter your username and password credentials. Its your choice.
3) Setup Your ADO Command Object to Define your data retrieval query:
'Create a Command object.
Private myCmd As SqlCommand
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT FirstName, LastName FROM Employees"
'Open the connection.
myConn.Open()
Note: Subsitute CommandText string for your actual query based upon your own database schema.
4) Read, Fetch, Display Data using the SQLDataReader Object:
Private results As String
Private myReader As SqlDataReader
myReader = myCmd.ExecuteReader()
'Traverse the DataSet and Display in GUi for Example:
Do While myReader.Read()
results = results & myReader.GetString(0) & vbTab & _
myReader.GetString(1) & vbLf
Loop
'Display results.
MsgBox(results)
5) Gracefully Close all Objects Used:
' Close the reader and the database connection.
myReader.Close()
myConn.Close()
Note - You'll need to consult microsoft for further connection string formats, since I don't have enough info. But this should clarify the actual big picture steps for you.
Regards,
Scott

Error while uploading Excel data to SQL server 2005

I have a requirement to upload excel data into a SQL Server 2005 table. Initially I copied Excel data to a temp table and based on condition I have updated and inserted records into the SQL Server table.
Everything works fine in my local system. Same program I moved to quality server there I faced this error
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
Then we installed 'Microsoft.ACE.OLEDB.12.0 provider on quality server. Now this error appearing
The Microsoft Office Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data."
Kindly help me out to solve this issue.
SqlConnection con = new SqlConnection();
con = SQLManager.openSQLConnection();
string path = FileUpload1.PostedFile.FileName;
string excelConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0 Xml;Persist Security Info=False";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
OleDbCommand cmd = new OleDbCommand("Select [CM_CODE],[CM_NAME],[CM_ADD1],[CM_ADD2],[CM_ADD3],[CM_ADD4],[CM_CITY],[CM_PHONE],[CM_CG_CODE],[CM_CO_CODE],[CM_EXPIRY_DATE],[CM_PINCODE],[CM_EMAIL] from [Sheet1$]", excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
Regards,
Sathya
This is most likely a file permissions issue. Does the account that the program runs under have folder level permission to read files from the file location.
Find out which account the program runs under and then add appropriate access permissions to the folder where you upload your excel files.
If it's IIS then whichever account your app pool runs under needs write access to the folder that you upload to. Permissions can usually only be set by web server admin.
Wing
Finally i found out the solution.
I have created one folder under my application folder in Quality server.
Uploaded excel sheets wil get stored under this new folder.
and that path i am referring while updating my SQL server database.
Everything works fine now...
Thank u all for your support
Regards,
Sathya

Access remote sql server using VB.NET

I've found a few examples of using vb.net to access an sql database, so far none of them have worked . They all involve using DataReaders. Maybe its the fact that the sql db is not on the same machine as the application.
I was just wondering if anyone had a more comprehensive example of using VB.NET to access a remote sql server.
Thanks!
EDIT:
I've received a few helpful comments an replies already. So far my connection string looks like:
"server=sqlblah.myhost.com;uid=myuser;pwd=pass;database=testdb"
Probably also good to mention their is no editing of the tables a this point, just reading.
Check out the SQLClient class.
One convienent way to access a SQL database is to fill a DataSet object with query data with a DataAdapter object.
Dim sSQL As String = "SELECT * FROM ???"
Dim conn As New SqlClient.SqlConnection("connection string")
Dim da As New SqlClient.DataAdapter(sSQL, conn)
Dim ds As New DataSet
da.Fill(ds, "TABLE NAME")
You can then access the "TABLE NAME" table in the DataSet object.
The "connection string" is obviously your SQL connection string.
Use the sSQL string to query as necessary.
Quick side note - a helpful tool for creating connection strings:-
Open up a text document (notepad, wordpad etc) and save a blank document with the extension ".UDL".
This will give you a "Data link Properties" mini app.
Open up the app and change the provider in the "Provider" to whichever provider you need (in this case OLE DB Provider for SQL Server).
You then need to build up the connection in the connection tab.
Once you have chosen the criteria (ServerName (the drop down list will show you all visible Servers), Security permissions,Database (this drop down list will be populated based on the server chosen)) you can test your connection (to make sure you have permissions etc).
Click ok to close the App, rename the file to have a ".txt" extension and re-open in a text editor, hey presto, one built up connection string (as below).
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=YOURDBNAME;Data Source=YOURSERVERNAME