Can we access database without SQL Server Management Studio in c# - c#

I created a database name="records" with the help of SQL Server Management Studio (SSMS). Now my question if I uninstall SSMS then can I still access the database from a C# program? If yes, then then is the process for connectivity still same as given below?
SqlConnection cnn ;
string connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
cnn = new SqlConnection(connetionString);

Management Studio is just a GUI client for interacting with a database. It is separate from the actual database.
In other words: you don't need Management Studio to programmatically access a MS SQL database.

Shortest answer, yes.
The management tools are an optional extra, all you really need is the database server.

Related

How do I used a database I created in MS SQL Server 2008 R2 with a VS C# Project?

I created a new database using SQL Server 2008 R2 by using the Management Studio. The connection says (local) and I am using Windows Authentication (though I installed with mixed mode).
My questions are:
How do I connect to the DB via my C# application -
The only time I ever have done this before I just used VS Menu > Tools > Connect to DB and the drop down saw my database and connected, then right clicked on it and grabbed the connection string for use in connecting. However I'm thinking because its (local) I don't have that option.
As per Q#1 I am assuming the database file is being stored somewhere locally - I am wondering how to find that location and how I can include it with my application
Edit** Per comment: VSMenu-> View-> Server Explorer and then use add connection to connect to your local SQL Server instance and then use the database you created from the databases dropdown, and from advance settings copy the connection string created by the connection dialog
This is what I am looking for but I am missing the step during "add connection" where do I find my SQL Server I created locally? As mentioned before I have no idea where it is stored or how to find it
MSDN has an example in the SQLConnection documentation
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand(queryString, connection))
{
command.Connection.Open();
command.ExecuteNonQuery();
}
You can then optionally use a SqlDataAdapter.
You need to use ADO.NET, which is comprised of a connection object (SqlConnection), command object (SqlCommand), parameters objects (SqlParameter) and data sets (DataSet) or data readers (SqlDataReader).
Read A Beginner's Tutorial for Understanding ADO.NET.

Can't connect to my SQL Server database?

I'm creating a very basic CRUD desktop winforms application in C#/.NET 4.0.
Letting Visual Studio auto-generate the fields for the table I'd like to perform my CRUD operations on works just fine, but I'm running into problems when I try and interact with it manually with my own SQL queries.
The auto-generated fields are using the connection string:
Data Source=|DataDirectory|\Data Analysis.sdf
If I try and do:
SqlConnection conn = new SqlConnection(#"Data Source=|DataDirectory|\Data Analysis.sdf");
conn.Open();
It just hangs. What am I missing?
That's a connection string for a SQL Server Compact Edition (CE) database (everything stored inside a single .sdf file) - is that what you're using?
If so : in that case, you'd have to use SqlCeConnection (not a SqlConnection - that's for "grown-up" SQL Server version - not CE)
Maybe try adding some more options to the connection string:
Persist Security Info=False;
File Mode=shared read;
Believe you've specified a relative path to the .sdf file, where you might need to get the executable's runtime folder from System.Environment.CurrentDirectory and prepend it to the filename.

asp.net c# connect to local Db vs2008

I'm making an ASP.net with c# webapp using VS 2008, and I added a new sql database item to my project. I added tables to the databse. In the database explorer the test connection works. I guess I have two questions. One:In the application, how does one connect to the database using a connection string? or what connection string should I use? Second: How do I add a username and password to the database?
Right now I'm using this connection string in the web.config file, but when I run the app it times out and says it can't make a connection. The error is on the conn.open line.
add name="ReportsConnectionString" connectionString="Data Source=(local); Initial Catalog=REPORTS;Integrated Security=True" providerName="System.Data.SqlClient"
I have this code in one of my page's codebehind.
string sqlquery = "SELECT * FROM reportitems";
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ReportsConnectionString"].ConnectionString))
{
conn.Open();
using (SqlCommand comm = new SqlCommand(sqlquery, conn))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(comm))
{
DataSet ds = new DataSet();
adapter.Fill(ds, "reportitems");
DataRowCollection dra = ds.Tables["reportitems"].Rows;
foreach (DataRow dr in dra)
{
string DRZ = dr[0].ToString();
//more stuff here
}
}
}
}
Usually SqlServer Express is reachable on your local PC using this syntax for the Data Source parameter yourpcname\SQLEXPRESS. To be sure start Management Studio and look at the Server Name request.
For the security part of your question, I suppose that you don't want the Integrated Security option (Windows User), but you want a SQLServer user. In this case you could use the User ID and Password parameters for the connection string:
Data Source=MYPC\SQLEXPRESS;Initial Catalog=REPORTS;User Id=MYNAME;Password=MYPASS;
However, this works only after you have added this user to the SQLServer.
You could use the interface of Management Studio app or you could execute a script like this
USE [master]
GO
CREATE LOGIN [MYNAME] WITH PASSWORD=N'MYPASS', DEFAULT_DATABASE=[master]
GO
USE [REPORTS]
GO
CREATE USER [MYNAME] FOR LOGIN [MYNAME]
GO
The Integrated Security=True part of the connectionstring means that the server will use the credentials of the app pool running the site, and you don't need to specify username or password. The app pool identiy will, however, need to have access to your database.
Visit http://www.connectionstrings.com/ for a good primer on various ways to set the connection string for various applications. That'll show you why (local) didn't work but .\SQLEXPRESS did and how to add username and password to it. Here's an example lifted from http://www.connectionstrings.com/sql-server-2008
Data Source=myServerAddress;Initial Catalog=myDataBase;User
Id=myUsername;Password=myPassword;
As others have said, you need a SqlExpress engine running as .mdf is not a flat file. It is a SQL server express database file and you need to connect to it.
But what have not said is that a Database in your App_Data folder needs to be attached to the SqlServer instance. This step is only done once in the first connection.
In http://www.connectionstrings.com/sql-server-2008 you will find an example in the "Attach a database file, located in the data directory, on connect to a local SQL Server Express instance" section that looks like this:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
Also you can read this: http://msdn.microsoft.com/en-us/library/ms247257.aspx
I believe that you will need to run some scripts and stuff like that to create a user and assign permissions to this user in this database, and then change the connection string (once the database attached), so I don't see a point in having the database in the App_Data folder. I believe it should be better if since the beginning you create your database using the SqlServer tools and connect to it from your application.

C# and SQL Server 2008 R2: Finding DB Address and connection string

I have SQL Server 2008 R2 installed and have the necessary databases created. Now I am trying to connect to the server through C# and failing miserably. I have tried several connection string formats from connectionstrings.com, but I still cannot connect to the database. This is the format I'm assuming I'm to use:
public static void connect()
{
string conString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword";
SqlConnection con = new SqlConnection(conString);
try
{
con.Open();
}
catch (Exception e)
{
Console.WriteLine("Unable to connect to database");
}
}
But I can't seem to identify the correct address and authentication (using windows authentication). How can I find the address in MSSM, and how would I properly use windows authentication?
Thanks so much.
Note: I am using Visual Studio 2010
Josh, you may want to consider using System.Data.SqlClient.SqlConnectionStringBuilder so you don't have to worry about the correct format.
edit: and when I actually look at your connection string, you say you're attempting Windows Authentication, but you provide a username and password. Instead, you want to do something like this:
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
I found a way to cheat and find the connection string. Adding a new ADO.NET Entity Data Model in Visual Studio 2010 and importing a database from SQL 2008 R2 actually lists the connection string in the dialog box! I'm still going to use the SqlConnectionStringBuilder, but now I have the elements I need. Thanks for the input!

Enable remote connections to sql express with a script

I am deploying an application with sql server express 2008. In the prerequisites section of my application I have included:
As a result when a user installs my application it will install sql express as well.
Then I will be able to connect to that database engine as:
try
{
// database should be in the same network
SqlConnection conn =
new SqlConnection(#"Data Source=.\sqlexpress; Integrated Security=True");
conn.Open();
MessageBox.Show("Connection succesfull");
}
catch
{
MessageBox.Show("Unable to connect");
}
Now when I install a different application(client version) I will like to be able to connect to that database engine. I managed to connect to it by doing something like:
try
{
SqlConnection conn =
new SqlConnection(#"Data Source=192.168.0.120\sqlexpress,22559; USER=sa; PASSWORD=*********");
conn.Open();
MessageBox.Show("Connection succesfull");
}
catch
{
MessageBox.Show("Unable to connect");
}
In order for that code to work I had to do the following:
So my question is:
How could I configure this with code? When I deploy my application I want my application to install sql express like it does but I also whant to enable tcp/IP connections, enable some ports and lastly create a password for the account "SA" because I am not able to connect to the database remotly if the sa account does not have a password.
Or maybe I am asking to much and I am doing the wrong thing. perhaps I should do all this just for the database that I am planing on deploying not the database engine. whatever is easier. I have had a hard time deploying this maybe it will be eassier to deoploy a local database along with a wcf service in order to create CRUD operations on the local database remotely.
EIDT
I found this 3 links that claim on doing something similar and I still cannot make it work.
1) http://support.microsoft.com/kb/839980
2) http://social.msdn.microsoft.com/Forums/en-US/sqlexpress/thread/c7d3c3af-2b1e-4273-afe9-0669dcb7bd02/
3) http://www.sql-questions.com/microsoft/SQL-Server/34211977/can-not-connect-to-sql-2008-express-on-same-lan.aspx
downloaded sql server express 2008 (SQLEXPR32_x86_ENU.exe) and place it in the root of my c drive. then I install it with the following parameters:
C:\SQLEXPR32_x86_ENU.exe /q /hideconsole /action=Install /features=SQL /instancename=SQLEXPRESS /enableranu=1 /sqlsvcaccount="NT Authority\Network Service" /AddCurrentUserAsSqlAdmin /skiprules=RebootRequiredCheck /TCPENABLED=1
I add /TCPENABLED=1 in order to enable TCP/IP
I suggest you to create modified bootstrapper package to install Sql Server 2005 Express with customzation.
As an alternative, you can also use a custom action in your installer to change the targeted server using SMO.
Something like this:
Server server = new Server( "ServerName\\InstanceName" );
server.ConnectionContext.Connect();
server.Settings.LoginMode = ServerLoginMode.Mixed;
server.Settings.Alter();
We use SMO object to create user login and associate user to our created application database.. even run sql script to create database if database is not available..
Refer these links:
Configuring SQL Express During Installation
Configuring SQL Server when included as a requirement
Note: Create your sql server connection string settings in App.config file rather than putting hardcore in code.. this will help you customize application first run customization e.g. database creation.
These might be of some help, I've had it on my todo list for a while for the computers I have to setup for my app to run with Sql Server 2008 Express. It's basically a way to setup a script that the SQL08exp installer will read and automate a lot of the setup according to what you set in the script.
http://digitalformula.net/articles/how-to-perform-an-unattended-installation-of-sql-server-2008-express/
http://blogesh.wordpress.com/2008/09/23/silent-install-of-sql-server-2008/

Categories