简体   繁体   English

数据库连接字符串asp.net和c#的语法错误

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

I have written code to connect to insert asp.net form data into an SQL server, created in Microsoft Visual Studio 2015, using c#. 我已经编写了代码,以使用c#将asp.net表单数据插入到在Microsoft Visual Studio 2015中创建的SQL服务器中进行连接。 The issue I am having now is that I am unable to test if form works due to a Synax error. 我现在遇到的问题是,由于Synax错误,我无法测试表单是否正常工作。 There are spaces between the text file, I am not sure if that might be the reason why there is an error? 文本文件之间有空格,我不确定这是否是导致错误的原因? Or misplaced connection file? 或放错位置的连接文件? I copied the pathing directly from Microsoft Access. 我直接从Microsoft Access复制了路径。 Any advice would be greatly appreciated. 任何建议将不胜感激。

Error: 错误:

Compiler Error Message: CS1003: Syntax error, ',' expected 编译器错误消息:CS1003:语法错误,“,”预期

Source Error: 源错误:

Line 11: public partial class _Default : System.Web.UI.Page 第11行:公共局部类_Default:System.Web.UI.Page

Line 12: { 第12行:{

Line 13: 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"); 第13行:SqlConnection con =新的SqlConnection(@“数据源=(LocalDB)\\ MSSQLLocalDB; AttachDbFilename =” C:\\ Users \\ P \\ Docs \\ Visual Studio 2015 \\ WebSites \\ WebSite2 \\ App_Data \\ Database.mdf“;集成安全性=真正”);

Line 14: protected void Page_Load(object sender, EventArgs e) 第14行:受保护的无效Page_Load(对象发送者,EventArgs e)

Line 15: { 第15行:{

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

This is the form code in c# 这是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();
    }
}

Should be 应该

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")

Because it isnt clear for the compiler what you mean. 因为对于编译器尚不清楚您的意思。 So we use "\\" as a signal that the following char should be interpreted as... well a char - and not as a keyword. 因此,我们使用“ \\”作为信号,表示以下字符应被解释为...以及一个字符,而不是关键字。

Otherwise " is interpreted as start/end of a string which results in string one: "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\\" 否则,“被解释为字符串的开始/结束,导致字符串一: ” Data Source =(LocalDB)\\ MSSQLLocalDB; AttachDbFilename = \\“

"unlogical" data (because not start or end sign): C: \\Users\\P\\Docs\\Visual Studio 2015\\WebSites\\WebSite2\\App_Data\\Database.mdf\\ “不合逻辑的”数据(因为没有开始或结束符号):C:\\ Users \\ P \\ Docs \\ Visual Studio 2015 \\ WebSites \\ WebSite2 \\ App_Data \\ Database.mdf \\

and string two: ;Integrated Security=True 和字符串二: ; Integrated Security = True

Other possibility: 其他可能性:

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")

Anyways using a web.config/app.config is way better for your purpose. 无论如何,使用web.config / app.config更好地满足您的目的。

You're using " two times in the same string I don't think they'll work even prepending @ . Try using \\" also a connection string should be in the app.config or web.config file. 您在同一字符串中使用了"两次,我认为即使在@之前它们也不起作用。尝试使用\\"并且连接字符串也应位于app.configweb.config文件中。 Never hardcoded in your source. 切勿在源中进行硬编码

Use connection strings and the web.config for easing changes. 使用连接字符串和web.config简化更改。

web.config example: 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>

Change your code to: 将您的代码更改为:

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