Reputation: 51
I am trying to connect my project in visual studio to my database using SQL Server 2012. this is my code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CollegeManagementSystem.lib
{
public class DatabaseConnection
{
public static SqlConnection Con;
public static SqlConnection GlobalConnection()
{
string ConString = @"Data Source=DESKTOP-JTVC4I1\\SQLEXPRESS;" + Global.ServerName + ";Initial Catalog=College" + Global.Database + ";Integrated Security=True;User Id=sa" + Global.SUserId + ";Password=123" + Global.SPassword + ";Connection Timeout=1000";
Con = new SqlConnection();
Con.ConnectionString = ConString;
if (Con.State == ConnectionState.Open)
{
Con.Close();
}
Con.Open();
return Con;
}
the name of my database is college.db, i am also confused i have a database in the server called master.db that has the same table i have created in my database college.db but i did not create it.
and I get the error :
A Network-related or instance-specific error occurred when 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.
I am running my SQL Server Locally. How can i fix this?
Upvotes: 0
Views: 461
Reputation: 7352
Change your connection string like below. Because Global.ServerName
already have the server name but in your code this get duplicated. Also I think Global.Database
, Global.SUserId
, Global.SPassword
are duplicated.
string ConString = @"Data Source=" + Global.ServerName +
";Initial Catalog=" + Global.Database +
";Integrated Security=False;User Id=" + Global.SUserId +
";Password=" + Global.SPassword + ";Connection Timeout=1000";
Upvotes: 0
Reputation: 1468
Your connection string's Integrated Security=True
. This means the User Id
and Password
attributes you specified will be ignored and the current Logged Windows Identity will be used.
Try changing your connection string to Integrated Security=False
More details can be found at MSDN SqlConnection.ConnectionString Property
When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication. Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true. If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used.
Upvotes: 1
Reputation: 156
You could try this :
string ConString = "Server=DESKTOP-JTVC4I1\\SQLEXPRESS;Database=College;User Id=sa;Password=123;"
Also make sure your database name is correct. In description you said that your database name is "college" but in connection string you have used "College".
Upvotes: 0