繁体   English   中英

插入数据库C#连接字符串错误

[英]Insert into database c# connection string error

尝试在数据库中设置插入命令时遇到错误,它似乎与连接字符串有关。 我对这一切都非常陌生,并尝试获取正确的代码以便上载到我的数据库中,并假设我使用的语法可能是错误的并且是错误的原因。

这是更清晰的代码:

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;

namespace ComputingProjectwh.TestPages._1._Further_Mechanics
{
    public partial class Moments_and_Energy_Test1 : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Submit_Click(object sender, EventArgs e)
        {
            if (!this.IsValid)
                return;
            int score = 0;
            List<RadioButtonList> list = new List<RadioButtonList>() { RadioButtonList1, RadioButtonList2, RadioButtonList3, RadioButtonList4, RadioButtonList5, RadioButtonList6, RadioButtonList7, RadioButtonList8, RadioButtonList9, RadioButtonList10 };
            foreach (var element in list)
            {
                if (element.SelectedValue == "Correct")
                {
                    score++;
                }

            }
            Response.Write("you scored: " + score);
            Button1.Visible = false;

            if (score != 0);
            {
                SqlConnection sqlConnection1 = new SqlConnection (@"Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-ComputingProjectwh-20170404101246.mdf;InitialCatalog=aspnet-ComputingProjectwh-20170404101246;IntegratedSecurity=True");

                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "INSERT AspNetUserTestScores (Id, MomentAndEnergyTestScore) VALUES (Id, score)";
                cmd.Connection = sqlConnection1;

                sqlConnection1.Open();
                cmd.ExecuteNonQuery();
                sqlConnection1.Close();


            }

        }
    }
}

在此处输入图片说明

我真的不确定是什么问题,并且似乎无法在互联网上找到答案。 任何帮助将不胜感激。

连接到MSSQL时,没有initialcatalog ,您正在使用错误的连接字符串。

这是正确的语法:

  Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

或者在您的情况下,对于可信连接:

  Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

使用您的数据:

 SqlConnection sqlConnection1 = new SqlConnection("Server=LocalDb;Database=aspnet-ComputingProjectwh-20170404101246.mdf;Trusted_Connection=True;");

InitialCatalog是两个单独的单词initial catalog

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM