[英]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.