两个常见错误使此网站无法正常工作



这是一个非常简单的网站,用户可以填写输入并将其保存在sql数据库中,还可以发送电子邮件。然而,我有两个错误,这是第一个:

尝试为文件附加自动命名的数据库C: \用户。。。\Desktop \Dpp2012New\App_Data\dpdatebase.mdf失败。A.存在具有相同名称的数据库,或者指定的文件不能已打开,或者位于UNC共享上。

描述:在执行期间发生未处理的异常当前web请求。请查看堆栈跟踪以了解更多信息有关错误的信息以及错误在代码中的来源。

异常详细信息:系统。数据SqlClient。SqlException:试图附加文件的自动命名数据库C: \用户。。。\Desktop \Dpp2012New\App_Data\dpdatebase.mdf失败。A.存在具有相同名称的数据库,或者指定的文件不能已打开,或者位于UNC共享上。

这是第二个:

类型为"System"的未处理异常。数据SqlClient。SqlException发生在系统中。Data.dll

显示:

"(".sql 附近的语法不正确

得到代码cmd.ExecuteNonQuery();

以下是我的代码:

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source= LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersuserDocumentsVisual Studio     2015WebSitesWebSite4App_DataDatabase.mdf;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.CommandText = "save ('"+txtName.Text+"','"+txtLtName.Text+"','"+compName.Text+"','"+address0ne.Text+"','"+addressTwo.Text+"','"+cityName.Text+"','"+reionName.Text+"','"+postCode.Text+"','"+countryName.Text+"','"+emailAddress.Text+"')";
        cmd.ExecuteNonQuery();
        con.Close();
    }
    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {
    }
}

我尝试过一些解决方案,但都不起作用。任何帮助都将不胜感激。

cmd.CommandText = @"INSERT INTO your_table_name(Name, LtName, CompName, AdOne,
AdTwo, City, ReionName, PostCode, CountryName, EmailAddr)
Values(@Name, @LtName, @CompName, @AdOne, @AdTwo, @City, @ReionName, 
@PostCode, @CountryName, @EmailAddr");
cmd.Parameters.Add("@Name", txtName.Text);
cmd.Parameters.Add("@LtName", LtName.Text);

等等。。。

这是插入语句还是更新语句?至少,您应该在sql中使用参数。。。但是您也应该使用using语句

最新更新