简体   繁体   中英

Insert into database c# connection string error

i am encountering an error when trying to set up an insert command into my database, it appears to be with the connection string. I am extremely new to all this and am trying to get the correct code in order to upload into my database and assume that the syntax i am using may be wrong and the cause of the error.

Here is the code a little bit clearer:

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();


            }

        }
    }
}

在此处输入图片说明

I am really not sure what the problem is and cant seem to find an answer on the internet. Any help would be greatly appreciated.

When connecting to MSSQL, there is no initialcatalog , You are using a wrong connection string.

This is the correct syntax:

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

Or in your case, for trusted connection:

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

With your data:

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

InitialCatalog是两个单独的单词initial catalog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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