Eri Agbayani Eri Agbayani - 3 years ago 267
SQL Question

Connecting SQL Server 2012 database to Visual Studio 2012

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download