简体   繁体   English

为什么在尝试与SQL Server建立连接时出现此错误?

[英]Why I'm getting this error when trying to establish a connection with SQL Server?

I'm trying to insert values in the table but when I click on the SignUp button it gives me error 我正在尝试在表格中插入值,但是当我单击“注册”按钮时,它给了我错误

An attempt to attach an auto-named database for file Database.mdf failed. 尝试为文件Database.mdf附加自动命名的数据库失败。 A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. 存在具有相同名称的数据库,或者无法打开指定的文件,或者该数据库位于UNC共享上。

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 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 Database.mdf failed. 异常详细信息:System.Data.SqlClient.SqlException:尝试为文件Database.mdf附加自动命名的数据库失败。 A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. 存在具有相同名称的数据库,或者无法打开指定的文件,或者该数据库位于UNC共享上。

Source Error: 源错误:

Line 22: string con =ConfigurationManager.ConnectionStrings["connection"].ConnectionString; 第22行:字符串con = ConfigurationManager.ConnectionStrings [“ connection”]。ConnectionString;
Line 23: SqlConnection conn = new SqlConnection(con); 第23行:SqlConnection conn =新的SqlConnection(con);
Line 24: conn.Open(); 第24行:conn.Open(); //error line //错误行
Line 25: if (selectques.SelectedItem.Text == "Write your own question?") 第25行:if(selectques.SelectedItem.Text ==“写您自己的问题?”)
Line 26: { 第26行:{

My button event 我的按钮事件

protected void signup_Click(object sender, EventArgs e)
{
   string con = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
   SqlConnection conn = new SqlConnection(con);

   conn.Open();

   if (selectques.SelectedItem.Text == "Write your own question?")
   {
      SqlCommand cmd = new SqlCommand("insert into registration values('" + username + "','" + passwrd + "','" + emailadd + "','" + alterquestion + "','" + securityanswer + "'", conn);
      cmd.ExecuteNonQuery();
      conn.Close();
   }
   else
   {
      SqlCommand cmd = new SqlCommand("insert into registration values('" + username + "','" + passwrd + "','" + emailadd + "','" + selectques + "','" + securityanswer + "'", conn);
      cmd.ExecuteNonQuery();
      conn.Close();
   }

And my web.config connectionstring 和我的web.config连接web.config

<configuration>
    <appSettings/>
  <connectionStrings>
    <add name="connection" 
         connectionString="server=.\sqlexpress; AttachdbFilename=Database.mdf; integrated security=true; user instance=true"/>
  </connectionStrings>

This part of your connection string is wrong: 您的连接字符串的这一部分是错误的:

AttachdbFilename=Database.mdf

AttachdbFilename needs a full path to the MDF file, not just its name. AttachdbFilename需要MDF文件的完整路径,而不仅仅是其名称。

Source: 资源:
Connecting to SQL Server Express User Instances (ADO.NET) 连接到SQL Server Express用户实例(ADO.NET)

Quote from the link: 从链接引用:

The AttachDbFileName connection string keyword is used to attach the primary database file (.mdf), which must include the full path name. AttachDbFileName连接字符串关键字用于附加主数据库文件(.mdf),该文件必须包含完整路径名。

change: 更改:

AttachdbFilename=Database.mdf

to this: 对此:

AttachdbFilename=|DataDirectory|Database.mdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试建立与SQL Server Express的连接时出错 - Error trying to establish connection to SQL Server Express 为什么在使用MySql时SQL Server连接错误? - Why SQL Server connection error when I'm using MySql? 尝试从我的应用程序建立连接时出现 SQL 错误 - SQL error when trying to establish connection from my application 我正在尝试通过 C# win forms 应用程序与 ms sql 建立连接 - I'm trying to establish connection with ms sql via C# win forms app 我正在尝试从SQL Server Compact数据库中读取数据,但是我一直收到相同的错误 - I'm trying to read data from my SQL Server Compact database, but I keep getting the same error 尝试在运行时创建动态对象列表时,为什么会出现编译错误? - Why am I getting a compile error when I'm trying to create a list of a dynamic object at runtime? 无法与SQL Server建立连接 - Can not establish connection with the sql server 为什么尝试GetComponent时会出现null异常? - Why i'm getting null exception when trying to GetComponent? 我正在尝试在SQL Server上编辑数据表,但由于出现超时而不断收到关于不接受关键字或未连接的错误 - I'm trying to edit a datatable on a SQL server and i keep getting an error about either not accepting a keyword or not connecting due to a timeout 为什么我在Microsoft.SqlServer.Server命名空间出错? - Why i'm getting error for the Microsoft.SqlServer.Server namespace?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM