【发布时间】:2017-12-30 18:16:21
【问题描述】:
我正在做一个涉及将数据添加到现有数据库行中的 Web 应用程序,在我的情况下,我希望这样当我单击一个按钮时,数据会插入到具有 NULL 值的列中。
我有一个页面让用户可以选择旅行的日期和人数,当他们单击“立即预订”按钮时,日期和人员数据应添加到数据库中。
但是当我点击按钮时,这个错误显示为:
System.Data.SqlClient.SqlException: 'An attempt to attach an auto-named database for file C: \Users\xxx\Documents\xxx\Project\App_Data\Database.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.'
我的朋友说要解决这个错误,我需要删除我当前的数据库并重新重做整个数据库,但是我发现它非常耗时且乏味,因为我已经建立了一个大数据库。
这些是我的按钮代码:
protected void bookBtn_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\xxx\Documents\XXX\Project\App_Data\Database.mdf;Integrated Security=True");
//SqlCommand cmd = new SqlCommand("Booking",con);
//not sure if this is correct
SqlCommand cmd = new SqlCommand("Update Tours set date = + '"+dateTextbox.Text+"', person = '"+personDLL.Text+"' Where tourName=@tourName");
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("date", dateTextbox.Text);
cmd.Parameters.AddWithValue("person", personDLL.Text);
con.Open(); //error here
int k = cmd.ExecuteNonQuery();
if (k != 0)
{
lblmsg.Text = "Record Inserted Succesfully into the Database";
lblmsg.ForeColor = System.Drawing.Color.CornflowerBlue;
}
con.Close();
}
有什么方法可以解决以下错误而无需重做整个数据库?
*我只有一个数据库,没有重复的
*由于我没有 10 声望,stackOverflow 不允许我上传图片以获得更好的说明,对此感到抱歉
【问题讨论】:
-
为什么C:\Users\g_jes\...中的C:后面有空格?
-
如果我删除空间会有错误,数据库无法执行。我在@maddy23285 之前尝试过这个