簡體   English   中英

數據庫連接字符串asp.net和c#的語法錯誤

[英]Syntax Error at Database Connection String asp.net & c#

我已經編寫了代碼,以使用c#將asp.net表單數據插入到在Microsoft Visual Studio 2015中創建的SQL服務器中進行連接。 我現在遇到的問題是,由於Synax錯誤,我無法測試表單是否正常工作。 文本文件之間有空格,我不確定這是否是導致錯誤的原因? 或放錯位置的連接文件? 我直接從Microsoft Access復制了路徑。 任何建議將不勝感激。

錯誤:

編譯器錯誤消息:CS1003:語法錯誤,“,”預期

源錯誤:

第11行:公共局部類_Default:System.Web.UI.Page

第12行:{

第13行:SqlConnection con =新的SqlConnection(@“數據源=(LocalDB)\\ MSSQLLocalDB; AttachDbFilename =” C:\\ Users \\ P \\ Docs \\ Visual Studio 2015 \\ WebSites \\ WebSite2 \\ App_Data \\ Database.mdf“;集成安全性=真正”);

第14行:受保護的無效Page_Load(對象發送者,EventArgs e)

第15行:{

源文件:C:\\ Users \\ P \\ Docs \\ Visual Studio 2015 \\ WebSites \\ WebSite2 \\ Default.aspx.cs行:13

這是C#中的表單代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;


public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C: \Users\P\Docs\Visual Studio 2015\WebSites\WebSite2\App_Data\Database.mdf";Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.CommandText = "insert into Table values('" + pName.Text + "','" + pEmail.Text + "')";
        cmd.ExecuteNonQuery();

        con.Close();
    }
}

應該

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=\"C: \Users\P\Docs\Visual Studio 2015\WebSites\WebSite2\App_Data\Database.mdf\";Integrated Security=True")

因為對於編譯器尚不清楚您的意思。 因此,我們使用“ \\”作為信號,表示以下字符應被解釋為...以及一個字符,而不是關鍵字。

否則,“被解釋為字符串的開始/結束,導致字符串一: ” Data Source =(LocalDB)\\ MSSQLLocalDB; AttachDbFilename = \\“

“不合邏輯的”數據(因為沒有開始或結束符號):C:\\ Users \\ P \\ Docs \\ Visual Studio 2015 \\ WebSites \\ WebSite2 \\ App_Data \\ Database.mdf \\

和字符串二: ; Integrated Security = True

其他可能性:

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + @"C: \Users\P\Docs\Visual Studio 2015\WebSites\WebSite2\App_Data\Database.mdf" + ";Integrated Security=True")

無論如何,使用web.config / app.config更好地滿足您的目的。

您在同一字符串中使用了"兩次,我認為即使在@之前它們也不起作用。嘗試使用\\"並且連接字符串也應位於app.configweb.config文件中。 切勿在源中進行硬編碼

使用連接字符串和web.config簡化更改。

web.config示例:

<configuration>
    <connectionStrings>
        <add name="LoginDB" connectionString="Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\P\\Docs\\Visual Studio 2015\\WebSites\\WebSite2\\App_Data\\Database.mdf;Integrated Security=True" />
    </connectionStrings>
</configuration>

將您的代碼更改為:

SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["LoginDB"].ConnectionString);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM