Access Visual Studio 2008 server-based database from VB 6 project - database

Is it possible to access a Visual Studio 2008-created server-based database from a VB-6 application? If not, what is the best way to share a database between a VB-6 application and a C# application?

If you are referring to the SQL Server Express database that comes with Visual Studio 2008, you can access that with plain old ADO.
The connection strings for doing this are at http://connectionstrings.com/sql-server-2005. The most appropriate one is probably this one:
Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;
Uid=myUsername;Pwd=myPassword;
For SQL Server Express (which is what comes with VS2008), don't miss the server name syntax Servername\SQLEXPRESS where you substitute Servername with the name of the computer where the SQL Server 2005 Express installation resides.
The reference for programming against a SQL Server database using ADO is here:
http://msdn.microsoft.com/en-us/library/aa905875(SQL.80).aspx
Example for connecting to the SQL Server database is below (untested). Note that this one uses SQL Server authentication rather than Windows Authentication. Check your connection strings on http://www.connectionstrings.com. If SQL Native Client doesn't work, try one of the others.
' Initialize variables.
Dim cn As New ADODB.Connection
Dim connectionString As String
' Specify the OLE DB provider.
cn.Provider = "{SQL Native Client}"
' Specify connection string on Open method.
connectionString = Server=myMachineName\SQLEXPRESS;Database=myDataBase; _
Uid=myUsername;Pwd=myPassword;
cn.Open connectionString

I just added a "Service-based database" to a project. I go the following in my app.config, in the configuratonStrings element:
<add name="SerializeToSql.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
I don't know if ADO has user instance support. if it does, then you may be able to create
an equivalent connection string for ADO to use. Otherwise, you'll need to just create a "normal" database in SQL Server Express. You should be able to use that from ADO.

In order for other machines to see your SQL Express instance, you need to make sure it allows remote connections, the SQL Browser service is turned on, and is allowed through the firewall.
Fire up the SQL Server 2005 Surface Area Configuration tool: All Programs->Microsoft SQL Server 2005->Configuration Tools->SQL Server Surface Area Configuration.
Click Surface Area Configuration for Services and Connections
Click the 'Remote Connections' node and select 'Local and remote connections'. If you want to refer to the server as ServerName\MSSQLSERVER, you'll have to user named pips or TCP/IP and named pipes.
Click the SQL Server Browser node. Change the Startup type to Automatic and start the service.
For the firewall, make sure you make an exception for the SQL Server program (C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\sqlservr.exe) and for the SQL Server Browser program (c:\Program Files\Microsoft SQL Server\90\Shared\sqlbrowser.exe)

Related

Can't connect to Visual Studio 2015 SQL DB using Excel?

!
I've tried using ADODB connection string and also in the Excel Data Connection Wizard.
I'm getting this error:
[DBNETLIB][ConnectionOpen(Connect()).]SQL Server does not exist or access is denied.
I can see the server created in Visual Studio with its databases fine, no worries, in SQL Server but not connect in Excel. I can connect in Excel to databases created in SQL Server but not those created in VS (2015).
It's seems like a server issue rather than database one but here's the connection string's anyway:
This works fine for database's created in SQL Server:
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=DESKTOP-ODUI05F\MSSQL2012;Initial Catalog=MyDatabase
but the same string generates the above error when connecting to the server/database created in VS.
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=aspnet-Widly-20160823125202
The server name is: (LocalDB)\MSSQLLocalDB.
The database is generated in VS using EF.
Please help - driving me nuts.
Thank you, Simon
Solved!
Followed the solution linked below.
Even though it's an MSSQL Server DB you're trying to connect to, using Excel Data Connection Wizard you have to ignore SQL Database connection option and choose "Other/Advanced" instead. I recorded it in a macro which produced all the vital parts for the connection string as follows:
How to connect to localDB in Excel
Provider=SQLNCLI11;
Integrated Security=SSPI;
Persist Security Info=False;
Data Source=(LocalDB)\MSSQLLocalDB;
Initial Catalog=aspnet-PTL-20160827031609
Besides doing the dishes this is my biggest and only achievement today! :)

How to access SQL Server through internet?

I want to test a connection using SQL Server that can access through internet but I'm lost, I don't know where to start.
I know how to do it in MySQL but I want to know how to do it in SQL Server.
Thanks in advance.
One quick trick I know of, which works even on machines without SQL tools installed:
Create a new blank file somewhere and give it .udl for an extension. It should change to an icon with a table and a white page with 0s and 1s in the background.
Double-click on it to open the "Data Link Properties" dialog
On the Provider tab, choose "Microsoft OLEDB Provider for SQL Server"
On the Connection tab, fill in the remote server details and credentials
Click "Test Connection"
Where to start?
Try looking for the correct connection string here
You then need to find an example using the System.Data.SqlClient
By 'connect through the internet' do you mean connect from the internet to a SQL Server?
You need to open appropriate ports in the firewall and NAT them to your SQL Server.
To get you started, I would install a copy of SQL Server Express onto your local workstation and create an SQL Server instance with SQL Server Authentication. Also install SQL Server Management Studio which is a visual console through which you can manage your database and logins etc. They are available for free here:
http://www.microsoft.com/en-us/download/details.aspx?id=29062
Go into SQL Server Management Studio, create a database and a login username and password with sysadmin rights. Connect using the following code. The principles are similar to connect to an SQL Server database over the internet only there are more security issues to take into account.
Dim sConnectionString As String = "Server= MYCOMPUTERNAME\SQLEXPRESS;Database=DATABASE_NAME;User ID=USER_ID;Password=PASSWORD;Trusted_Connection=False;
Try
Dim mySqlConnection As New System.Data.SqlClient.SqlConnection()
mySqlConnection.ConnectionString = sConnectionString
mySqlConnection.Open()
MessageBox.Show("You have connected successfully to the SQL Server database.")
Catch SqlEx As SqlException
MessageBox.Show(SqlEx.Message & Constants.vbNewLine & Constants.vbNewLine & "You have NOT connected successfully to the SQL Server database.")
Catch ex As Exception
MessageBox.Show(ex.Message & Constants.vbNewLine & Constants.vbNewLine & "You have NOT connected successfully to the SQL Server database.")
End Try

Connect to local SQL Server Express 2008 instance from classic ASP, using SQLOLEDB

I am trying to set up a development testing server, Windows 2008 Server with IIS and SQL Server 2008 Express. The IIS and classic ASP (yeah, supporting antique projects) works fine, including connecting to remote SQL Server 2008 servers, via SQLOLEDB.
However, I fail to connect to the local SQL Server 2008 Express instance. It is installed as an instance (ie, PC-NAME\SQLEXPRESS), and user/pwd authorisation is enabled. I can successfully set up an ODBC entry, so the server works fine. The problem lies in getting the IIS-ASP-SQLOLEDB see the server and connect to it.
Simple connection string, which works on any remote SQL Server 2008 servers, fails at my local express server:
Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;User ID=userName;Password=_pwd_;Database=DbName
The error I get is
SQL Server does not exist or access denied.
The server error log does not contain any trace of unsuccessful login attempt, so I assume there is some problem resolving the data source (because of the instance name?), or the server is somehow inaccessible (protocol, security settings?) from the IIS process.
In SQL Server 2008 express edition it runs on dynamic ports. The solution is just go to properties of the TCP/IP and in the TCP Port just give whatever port you want your SQL Server to listen on.
strConn = "Provider=SQLOLEDB; Network Library=DBMSSOCN; Data Source=.\SQLEXPRESS; Connection Timeout=15;Packet Size=4096; Initial Catalog=DbName; User ID=userName; Password=_pwd_;"
Can you post how you access the connection object via ASP?

can't connect to the SQL database

I created a database in SQL Server Management Studio and I called it SalonDatabase. I tried to connect to the database in VS C#. For this I created two connectionString and only one of them work so I would like to know why.
This one works:
string connString = #"Data Source=.\SQLEXPRESS;initial catalog=SalonDatabase;Integrated Security=SSPI";
However, the following does not work even though I know for a fact that the file is located in that directory
string connString = #"Data Source=.\SQLEXPRESS;
Initial Catalog=C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\SALONDATABASE.MDF;Integrated Security=SSPI";
The error I'm getting is :
Cannot open database "C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\SALONDATABASE.MDF" requested by the login. The login failed. Login failed for user 'USER-PC\USER'
Now I logged in as administrator in my computer. I also read about the user instances in SQL server but I don't see that is the reason why the second connection fails.
The database is created using SQL Server Management Studio so do not use AttachDBFileName. You may use Server Explorer (from View Menu) tool of Visual Studio to obtain the connection string.
Add connection From Server-Explorer + Right mouse click on Data Connections + Add Connection + Select Microsoft Sql Server Data Source (change).
After adding connection, right mouse click to select/open properties windows.
Data Source=.\sqlexpress;Initial Catalog=SalonDatabase;Integrated Security=True
Modify your connection to 'attach' your mdf. Given that your .mdf path has spaces, be sure to enclose it in single quotes.
Server=.\SQLExpress;
AttachDbFilename='C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\SALONDATABASE.MDF';
Database=dbname;
Integrated Security=SSPI;
Trusted_Connection=Yes;
Initial Catalog is used to set the Default Database name when we Connect to a Server.
when you are passing the complete path of the database file the connection String makes a search for a database with name C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\SALONDATABASE.MDF
which actually does not exists therefore it fails.
you can use the initial approach and if you want to use full path then AttachDbFilename can be used as suggested by P.Campbell
By default, when working with SQL Server (any edition), the server part handles all the file-related chores for you. You just create a new database (or attach an existing one) and give it a logical database name and then you refer to that database solely via this database name - you don't care (and don't need to) about the actual files that make up your database.
That's why your first connection string works - you're telling SQL Server (Express) that you want to work with a database called SalonDatabase and SQL Server knows which files this concerns.
Microsoft introduced this notion of being able to "dynamically" create a user-instance of SQL Server Express and attaching a physical .MDF file (instead of referring to a database by logical name) as a developer tool to make things easier - but quite frankly, in my opinion, it's a rather messy concept and has lots of drawbacks.
In this scenario, you basically tell SQL Server in your connection string (using AttachDbFileName=salondatabase.mdf and with User Instance=True) that it should start a separate instance of SQL Server Express for your user context, and automagically attach that physical MDF file specified as its database, and provide you access to that.
As mentioned by another comment, it's a deprecated feature - SQL Server 2012 will bring a new, hopefully slicker concept of local databases. So in my opinion, you should try to avoid using this feature - it's more trouble than anything else.

SQL Server 2008 SSMS connection and http connection string?

I have a SQL Server 2008 on a server which goes out on the internet through a domain name computer.example.com, I want to develop a .net app on my PC and to connect to that database through SSMS - to create tables and so on...
And I still want a connection string which can be accessed from anywhere - I'm thinking at a http url or something like that - which will be consumed by the app.
What do I must to configure on the server so that I can connect via SSMS to the database (from my PC) and how do I get a connection string to that database?
you can use Sql Server Configuration Manager for this work. when you run this application, expand the SQL Server Network Configuration, after that you can see Protocols for sql server 2008. by click on it you can enable or disable TCP/IP.
your connection string can be like this :
connectionString="Database=DBName;Server = Server IP;Integrated Security=false;user id=sa; password=Pass;

Resources