Dear Sir,
I am getting error while connecting to the database.
Here I have created the database named MyWorkers.mdf by using the visual C# express edition by selecting the project menu and then add new item and then selecting the service based database option.
Now while I want to connect to that database using code I am getting error.
My code is as follows:
private void Form1_Load(object sender, EventArgs e)
{
string source="server=.//SQLEXPRESS; Integrated security=SSPI ; database=MyWorkers.mdf ;
sqlconnection conn =new sqlconnection(source);
conn.Open();
MessageBox.Show("Database is connected");
Conn.Close();
}
While compiling this code I am getting following error:
Error:
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)
But When I write following code I can successfully connect the database what the main problem please some body help me plzzzzzzzzz.
private void Form1_Load(object sender, EventArgs e)
{
string source = "DataSource=.\\SQLEXPRESS;AttachDBFilename=C:\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection conn = new SqlConnection(source);
conn.Open();
MessageBox.Show("Database is connected");
conn.Close();
}
Loading
Mayur DighePosted Sep 7, 2011, 11:20 AM
If you are getting same error after Correcting the CODE then don't panic its not error about you code , this error can occur if SqlServer.exe is not running.
if the process SQLServer.exe is not running then SQL SERVER Gives the error that you face currently.
SQLServer.exe is used as instance of SQL Server.
you can Run this from SQL Server Configuration Manager Wizard.(use above path)
Then check whether Sql Server Browser is running or not , if not then turn it on.
Figure Source : Internet
Rajesh PandeyPosted Aug 28, 2011, 1:15 AM
Sam HobbsPosted Aug 27, 2011, 4:13 PM
In the first connection string (the one that does not work) you have "database=MyWorkers.mdf". There is no qualification for the filename so it looks in the default location for the database. In your second connection string (the one that does work) you have "AttachDBFilename=C:\\MyWorkers.mdf" so it knows exactly where the file is at. Also note that the root of your C: is definitely not where it would expect (look for) the database.
Also note that in your first connection string you use "server=.//SQLEXPRESS" (with forward slashes) and in the other one you use "DataSource=.\\SQLEXPRESS" (with backslahes). Since the error message says that the "server was not found" the error seems to be that ".//SQLEXPRESS" is invalid; I assume that the problem is the forward slashes.
Satyapriya NayakPosted Aug 27, 2011, 10:41 AM
Create an app.config file.
First in the app.config file there you declare the connection string as below.
Then call the key name in your application as below
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
Iam giving a example how to show records in datagridview using this connection.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "employee";
con.Close();
}
}
}
Please Run the attachments also for the output.
Thanks
If this post helps you mark it as answer
Rajesh PandeyPosted Aug 27, 2011, 8:06 AM
Suthish NairPosted Aug 27, 2011, 2:05 AM
How to: Generate or find Connection String from Visual Studio