Create SQL Server database programmatically with C# - c#

I'm trying to follow this example but I get an Exception related to the connection string telling me that the server was not found or was not accessible. The tutorial itself tells me on step 5 to "Change the connection string to point to your computer running SQL Server". I don't know if my SQL Server is running or not and if it is I don't know what would be the name of the server. I know I installed SQL Server when I installed VS 2010 (I did a full installation), so it should be somewhere. I haven't changed anything in the SQL Server configuration so everything should be on what is default.

If you installed SQL Server along with VS 2010, you have SQL Server 2008 Express edition on your machine, and it is installed by default as a "SQLExpress" instance, so your connection string would have to be something like:
server=(local)\SQLExpress;database=(whatever_you_want);integrated security=SSPI;
This would expect that database you specify to already exist on your server.
If you want to programmatically create a database, you would have to connect to the master database
server=(local)\SQLExpress;database=master;integrated security=SSPI;
and then run a SQL statement something like this from your app:
CREATE DATABASE (newDatabaseName)

You can check under services (start/run/services.msc) whether your SQL server is running or not; or in your start menu under "sql server configuration manager". pls check if named pipes and tcp-ip are enabled.

Related

Application can't connect to copy of SQL Server database on other computer

I have created a C# application that connects to a SQL Server database which I created in SQL Server 2014 Management Studio. I need to send my app, my database, and instructions to get them working to someone for assessment.
My application connects perfectly on my computer with the connection string
Data Source=(local);Initial Catalog=ReportsDB;Integrated Security=True
However I am testing it on another computer and it will not connect with that same string.
I have installed SQL Server 2014 Express on that computer, and restored the database so it is identical to my main computer.
Any advice on how to make this work, or alternatively make it portable enough to submit?
The problem I had was that I assumed that connection strings were the same (apart from computer name) between SQLServer and SQLExpress however they are not.
My solution was rather hacky- I found the SQLExpress Connection String using VS on my second computer, replaced the computer name with the (local) keyword and then made an option in my application to switch between the default SQLExpress or SQLServer connection string so the user might change it manually. I also included an option to allow them to completely modify the connection string if neither default works. This is far from the most elegant solution, but with my limited understanding, it is passable to get the system working on a new computer.

Can't connect to SQL Server 2014 Express

I'm having trouble connecting to a freshly installed instance of SQL Server 2014 Express. I'm not even totally sure what information you would need to help, so please bear with me as I'm very much an amateur. If I can provide anymore information to help, I'd be glad to.
The error I get is always the same,
"A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: Named
Pipes Provider, error:40 - Could not open a connection to SQL Server)"
The server currently running SQL Server 2014 Express is Windows Server 2008 R2, with all patches sent out by Microsoft. This is a personal server at my home, so I can do whatever needs to be done with it to make this work.
To connect, I've tried using Visual Studio Community 2013, as well as the System.Data.SqlClient namespace in C# and in PowerShell.
I've made sure it isn't the firewall causing the problem. I set the rules properly, and when it still didn't work, I turned the firewall off completely. Same issue, no connection.
Here are a few examples of connection strings I've tried in .NET. I've removed the server, instance, userid, and password from these strings, and I'm sure I have those correct
Data Source=SERVERNAME\INSTANCENAME;Initial Catalog=Requests;Integrated Security=False;User ID=USERID;Password=PASSWORD
Data Source=SERVERNAME;Initial Catalog=Requests;Integrated Security=False;User ID=USERID;Password=PASSWORD
Server=SERVERNAME\INSTANCENAME; Database=DATABASE; User ID=USERID; Password=PASSWORD
Server=SERVERNAME; Database=DATABASE; User ID=USERID; Password=PASSWORD
If anyone has the time and patience to help a newbie sort this out, I'd really appreciate it. I'm using this setup to teach myself some SQL and ASP.NET MVC 5 development, and having a minilab at home would be awesome.
Thank you for your time.
First off, open services.msc and scroll down to SQL Server, and ensure its started.
If its not running modify to start automatically, and then start it.
Once it's running, close services panel, and run ssms.exe (management studio) and try connecting to Server Name: <machinename> or Server Name: <machinename\sql2014> [no <>]. You can also click the Server Name drop down list, and select BROWSE to see if your instance is listed - although I find this hit and miss.
Once you confirm its active and you are connected, follow this msdn page and follow the steps to enable tcp/ip connections.
From there you should have the details necessary to alter your connection string.
SQL Server Express does not have TCP/IP connections enabled by default, instead it uses Shared Memory which means that external/remote connections from another computer won't work.
Open SQL Server Configuration Manager (it's on your Start Menu) and choose Server configuration and enable TCP/IP. Also open the properties for TCP/IP and ensure you have both "Active" and "Enabled" set to True on the bindings you want to use.
I had a similar issue with a C# connection string that was no longer working after we migrated from SQL Server 2008 R2 on a Windows 2008 R2 Server to SQL Server 2014 on a Windows 2012 R2 Server.
I opened SQL Server Configuration Manager and Enabled Named Pipes, restarted the SQL Server and all was right with the world (at least for a few minutes!)
Make sure that your username and password are correct in case of Server Authentication mode.
Go to Sqlserver Management Configuration and start SQL server services if any are in stop mode.

Connecting SQL SERVER in C#.net on networked server

I have an existing application build in C#.net, using SQL SERVER 2005, earlier application and SQL was on same machine, but now I need to connect SQL which is on server and is networked to the machine on which application is installed.
So can any one tell me how to connect SQL to application , and what changes need to be made in connection string.
Thanks
EDIT : I am a bit new to c#.net.
currently my connection string is : public SqlConnection con = new sqlConnection("server=.;database=Database1;integrated security=sspi); so if I just put this will it directly connect to the SQL which is on other computer ?
The answer depends on whether you are using SQL Server authentication
Standard Security (SQL Server authentication)
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
or to connect to a specific instance on the server
Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;
Trusted Connection
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
http://connectionstrings.com/sql-server-2005
Server=.;
Means you were connecting to a default instance of sql2005 on the same machine.
Since 2005 SQL Server has allowed instances.
So you could have MyMachine/MyLiveDBMSInstance and MyMachine/MyTestDBMSInstance.
Basically as though you'd installed sql server twice. Default (ie unnamed) instances were a backwards compatibility measure, so DBAs didn't have to go around explaining what an instance was to mere programmers. :)
You would have connected to them with Server = MyMachine\MyLiveDBMSInstance or perhaps
Server = .\MyInstance dot being this localhost basically.
So you can't use . any more, you need tthe name of the machine, and if it's using a named instance, you'll need that as well.
i.e Server = RemoteMachine or Server = RemoteMachine\InstanceName
Course all this assumes the machine with the dbms is on is set up correctly and the windows user running the application has been allowed access.

Login fails when try to connect by app but not by the Management Studio

I have a WebSite that just don't connect to database. Both are on my local machine.
Using the same parameters (Host, User ID, Pwd and Database) I can connect with the MS SQL Server Management Studio.
What can by my problem??
Here goes the two ConnectionStrings I'm trying to use:
Data Source=VIVID-28\MSSQLSERVER1;Initial Catalog=DicionarioDados;Persist Security Info=True;User ID=VIVID-28\Development;Password=*******
Server=VIVID-28\MSSQLSERVER1;Database=DicionarioDados;Integrated Security=SSPI;User ID=VIVID-28\Development;Password=*******
VIVID-28 Is the name of my machine.
MSSQLSERVER1 Is the Instance.
Try removing the "Integrated Security=SSPI" section. That means that it uses Windows Authentication and hence will ignore the user name and password you are passing in your connection string.
To test your connection you can try using VS server explorer and by adding connection to your sql server database you can test whether connection can be made with given parameters or not. ones you able to connect you can select the connection and go to properties and copy the connection string and past it as your application connection string.
There can be many reasons of this , see the following similar question and see the links I have given in the post, follow the steps.
Why I am not able to connect to remote SQL Server from asp.net website whereas it is connecting from SQL Server Management Studio
Do you work with multiple SQL Servers?
Are you absolutely sure that the SQL Management studio is connecting to the same sql server / instance as your code? If you have a production and development server, you may have created the user on your production server, but not development, or the password may be incorrect.
Try resetting the password on your development server and see where that takes you. (:

Connecting to sql server database mdf file without installing sql server on client machine?

I am creating a window application that need to use sql server database.
I want to install this application to client machine without installing sql server so that my application can still connect to a database i.e mdf file that i will be providing at client system.
How can i connect to a database(mdf) on client machine through my window application without installing sql server. ?
I dont know is it possible or not.
If possible what will be the connection string in that case. Database need not be used in network.
Client mahine dont need any installation. Every thing needs to be run through pen drive
.mdf files are SQL Server database files. No other application can understand, read or update those files. If you need to open and mdf, you need a SQL instance. That instance can be local, can be an Express edition, or can be a remote one, doesn't matter. If your application needs a local SQL instance for it's own use then it can install SQL Server Express Edition when deployed.
You can try Sql Compact Edition or SqlLite. I think these are just a file based solution.
In that case you must have a server machine where your database files are stored. For that reason you have to use SQL Express Edition 2005 or 2008 in one machine and SQL server management studio to manage your database. Those are all free from Microsoft. The client machines will be connected through the connection string. Those machines don't need SQL instance or SQL server installed.
Regards..
You can search your MS-VS CD for SQLEXPRESS (please note the capital letters) and include it in your software when you create the set-up and your problem will be solved.
You don't have any other way except to use another database like access.
This database doesn't need its software.

Categories