简体   繁体   中英

System.Data.SqlClient.SqlException

protected void Button1_Click(object sender, EventArgs e)
{
    if (TextName.Text != "" && TextPass.Text != "" && TextRePass.Text != "" && TextAddr.Text != "" && TextPhn.Text != "")
    {
        SqlConnection i = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Study materials\Mobile Shop\App_Data\;Integrated Security=True;User Instance=True");
        i.Open();
        string q = "insert into Register values ('" + TextName.Text + "','" + TextRePass.Text + "','" + TextAddr.Text + "','" + TextPhn.Text + "','" + Label1.Text + "','" + Label2.Text + "')";

        SqlCommand cmd = new SqlCommand(q, i);
        cmd.ExecuteNonQuery();
        Label3.Text = "DETAILS ENTERED SUCCESSFULLY IN THE DATABASE!!";
    }
    else
    {
        Label4.Text = "ALL FIELDS ARE MANDATORY";
    }
}

I having a error here:

i.Open(); 

Error is:

An attempt to attach an auto-named database for file E:\\Study materials\\Mobile Shop\\App_Data\\ failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file E:\\Study materials\\Mobile Shop\\App_Data\\ failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

You need a database name in the Connection String:

SqlConnection i = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Study materials\Mobile Shop\App_Data\<DB Name Here>;Integrated Security=True;User Instance=True");
                                                                                                                           ^^^^^^^^^^^^

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