简体   繁体   中英

Visual Studio 2013 DB Creation C# a network related or instance specific error occurred while establishing connection with sqlserver

Hi I have been going through threads with the same error but cannot find any solution with visual studio 2013. When I test the connection with the database only it works and I can enter the records manually. But when I try using the code below it gives me the above mentioned error. I created the database with SQL. The original Connection string is

Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\fatimam\documents\visual studio 2013\Projects\BookingSystem\BookingSystem\App_Data\BookingsDb.mdf";Integrated Security=True

I suspect that my connection string in my code is not correctly formatted. I tried (LocalDB)\\v11.0 and gives me the same error as well. when I run the program and it gets to the myDBconnection.Open(); then it goes to the Catch block. Could you please help me resolve this connection problem? Thank you

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        using System.Data.Sql;
        using System.Data.OleDb;
        using System.Data.SqlClient;
        using System.Configuration;

        namespace BookingSystem
        {
        public partial class _Default : Page
        {
        protected void Page_Load(object sender, EventArgs e)
        {

        int id = 65;
        string title = "Mr";
        string name = "Name";
        string surname = "Surname";
        string company = "Company";
        string address = "address";
        string city = "Cape town";
        string postal = "7764";
        string contact = "0829875";
        string email = "email";

        try
        {
            string myDBCon = "Data Source=." + "\\" + "SQLEXPRESS;AttachDbFilename='C:" + "\\" + "Users" + "\\" + "fatimam" + "\\" + "Documents" + "\\" + "Visual Studio 2013" + "\\" + "Projects" + "\\" + "BookingSystem" + "\\" + "BookingSystem" + "\\" + "App_Data" + "\\" + "BookingsDb.mdf';Integrated Security=True;User Instance=True";
            //Creating connection to the Database
            System.Data.SqlClient.SqlConnection myDBconnection = new System.Data.SqlClient.SqlConnection();
            //Adding the connection string to the connection
            myDBconnection.ConnectionString = myDBCon;
            //Opening Connection
            myDBconnection.Open();
            //Creating a string to hold the sql query
            string insertSqlQuery = "INSERT INTO Customer (customer_id, title, name,surname,company,address,city,postal_code,contact,email) VALUES ('" + id + "','" + title + "','" + name + "','" + surname + "','" + company + "','" + address + "','" + city + "','" + postal + "',,'" + contact + "','" + email + "')";
            //Create a command in C# to perform the sql Query
            SqlCommand myInsertCommand = new SqlCommand(insertSqlQuery, myDBconnection);
            //Creating the process for performing the command
            SqlDataReader performCommand = myInsertCommand.ExecuteReader();
            //Executing the command or running the command
            performCommand.Read();
            //Closing the execution of the command
            performCommand.Close();
            //Closing the connection
            myDBconnection.Close();

        }

        catch (SqlException exp)
        {
            // Log what you need from here.
            throw new InvalidOperationException("Data could not be read", exp);
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}

}

In your connection string string myDBCon just use single \\ and use Server keyword instead instead of Data Source . and at the end of your connection string you can use Trusted_Connection=True; instead of both Integrated Security=True; and User Instance=True" . And an other important thing is YOURCOMPUTERNAME\\SQLEXPRESS .

Per the answer provided by user 'eu-ge-ne' here:

c# 2008 SQL Server Express Connection String

The recommended solution would be to remove "Integrated Security=True" from your connection string, and optionally add "Persist Security Info=True;".

According to MSDN:

Integrated Security - When false, User ID and Password are specified in the connect. When true, the current Windows account credentials are used for authentication.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM