必须声明标量变量

时间:2016-05-30 09:28:27

标签: c# xml

我正在开发一个项目来对XML进行deserilize并将其读取到数据库中。

我有以下代码:

query += ", NationalRegistrationDate = @NationalRegistrationDate";
        command1 = new SqlCommand(query, connection);
        DateTime myDate = DateTime.ParseExact(d, "yyyyMMdd",
        System.Globalization.CultureInfo.InvariantCulture);
        command1.Parameters.AddWithValue("@NationalRegistrationDate", myDate);

我收到此错误:

  

必须声明标量变量@NationalRegistrationDate

有谁知道我为什么会收到这个错误?

以下是代码的其余部分:

      using (var connection = new SqlConnection(connetionString))
        {
            try
            {
                connection.Open();
            }
           catch(Exception e)       
            {
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("DatabasConnection Done");
            DateTime datum = DateTime.Now;
            string LastChangedBy = "System";

            foreach (Person p in myPersons)
            {
                SqlCommand command1 = new SqlCommand();
                try
                {
                    command1 = Avreg(p.UnregistrationReason, p.GivenNameNumber, p.ProtectedIdentity, p.CitizenshipDate, p.NationalRegistrationDate, connection);

                command1.Parameters.AddWithValue("@PersonalIdentityNumber", string.Format("{0}{1}", p.PersonalIdentityNumber, p.SpecialIdentityNumber));
                command1.Parameters.AddWithValue("@FirstName", p.FirstName ?? (object)DBNull.Value);
                command1.Parameters.AddWithValue("@LastName", p.LastName ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@NationalRegistrationCountyCode", p.NationalRegistrationCountyCode ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@NationalRegistrationMunicipalityCode", p.NationalRegistrationMunicipalityCode ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@NationalRegistrationDistributionAddress1", p.NationalRegistrationDistributionAddress1 ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@NationalRegistrationDistributionAddress2", p.NationalRegistrationDistributionAddress2 ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@NationalRegistrationPostCode", p.NationalRegistrationPostCode ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@NationalRegistrationCity", p.NationalRegistrationCity ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@BirthCountyCode", p.BirthCountyCode ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@UnregistrationDate", p.UnregistrationDate ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@BirthParish", p.BirthParish ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@CitizenshipCode", p.CitizenshipCode ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@CitizenshipDate", p.CitizenshipDate ?? DBNull.Value.ToString());
                //command1.Parameters.AddWithValue("@NationalRegistrationDate", p.NationalRegistrationDate ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@ForeignDistrubtionAddress1", p.ForeignDistrubtionAddress1 ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@ForeignDistrubtionAddress2", p.ForeignDistrubtionAddress2 ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@ForeignDistrubtionAddress3", p.ForeignDistrubtionAddress3 ?? DBNull.Value.ToString());
                command1.Parameters.AddWithValue("@ForeignBirthCity", p.ForeignBirthCity ?? DBNull.Value.ToString());

                command1.Parameters.AddWithValue("@LastChangedBy", LastChangedBy);
                command1.Parameters.AddWithValue("@LastChangedDate", datum);

                command1.ExecuteNonQuery();

                Console.WriteLine(string.Format("{0}{1}", p.PersonalIdentityNumber, p.SpecialIdentityNumber));



                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

            }
        }

        Console.WriteLine("Done");
        Console.WriteLine("Alla fält uppdaterade");

    Console.ReadKey();

         }// Put a break-point here, then mouse-over PersonalIdentityNumber...  deserializedList contains everything if you need it
        catch (Exception e)
        {

            Console.Write(" ---- FEL VID INLÄSNINGEN ------  " + e.Message);
            Console.ReadKey();
        }

    }






    public static SqlCommand Avreg(string s, string t, string p, string c, string d, SqlConnection connection)
    {
        try
        {


        var query = "UPDATE Seamen SET FirstName = @FirstName, "+
            "LastName = @LastName, " +
            "NationalRegistrationCountyCode = @NationalRegistrationCountyCode, " +
            "NationalRegistrationMunicipalityCode = @NationalRegistrationMunicipalityCode, " +
            "NationalRegistrationDistributionAddress1 = @NationalRegistrationDistributionAddress1, " +
            "NationalRegistrationDistributionAddress2 = @NationalRegistrationDistributionAddress2, " +
            "UnregistrationDate = @UnregistrationDate, " +
            "NationalRegistrationPostCode = @NationalRegistrationPostCode, " +
            "NationalRegistrationCity = @NationalRegistrationCity, " +
            "BirthCountyCode = @BirthCountyCode, " +
            "BirthParish = @BirthParish, " +
            "CitizenshipCode = @CitizenshipCode, " +
            "ForeignDistrubtionAddress1 = @ForeignDistrubtionAddress1, " +
            "ForeignDistrubtionAddress2 = @ForeignDistrubtionAddress2, " +
            "ForeignDistrubtionAddress3 = @ForeignDistrubtionAddress3, " +
            "ForeignBirthCity = @ForeignBirthCity, " +
            "LastChangedBy = @LastChangedBy, " +
            "LastChangedDate = @LastChangedDate";

        SqlCommand command1;



        if (c == "0")
        {
            query += ", CitizenshipDate = null";
            command1 = new SqlCommand(query, connection);




        }
        else
        {
            query += ", CitizenshipDate = @CitizenshipDate";
            command1 = new SqlCommand(query, connection);

            command1.Parameters.AddWithValue("@CitizenshipDate", c ?? DBNull.Value.ToString());

        }

        if (d == "0")
        {
            query += ", NationalRegistrationDate = null;";
            command1 = new SqlCommand(query, connection);

        }
        else
        {
            query += ", NationalRegistrationDate = @NationalRegistrationDate";
            command1 = new SqlCommand(query, connection);
            DateTime myDate = DateTime.ParseExact(d, "yyyyMMdd",
            System.Globalization.CultureInfo.InvariantCulture);
            command1.Parameters.AddWithValue("@NationalRegistrationDate", myDate);

        }


        if (p == "J")
        {

            query = "UPDATE Seamen SET FirstName ='Skyddad personuppgift', " +
            "LastName = 'Se hjälptext', " +
            "ProtectedIdentity = '1', " +
            "NationalRegistrationCountyCode = NULL, " +
            "NationalRegistrationMunicipalityCode = NULL, " +
            "NationalRegistrationCoAddress = NULL, " +
                "NationalRegistrationDistributionAddress1 = NULL, " +
                "NationalRegistrationDistributionAddress2 = NULL, " +
            "UnregistrationDate = NULL, " +
            "NationalRegistrationPostCode = NULL, " +
            "NationalRegistrationCity = NULL, " +
            "BirthCountyCode = NULL, " +
            "BirthParish = NULL, " +
            "CitizenshipCode = NULL, " +
            //"CitizenshipDate = @CitizenshipDate, " +
            "NationalRegistrationDate = NULL, " +
            "ForeignDistrubtionAddress1 = NULL, " +
            "ForeignDistrubtionAddress2 = NULL, " +
            "ForeignDistrubtionAddress3 = NULL, " +
            "UnregistrationReason = NULL, " +
            "ForeignBirthCity = NULL, " +
            "LastChangedBy = @LastChangedBy, " +
            "GivenNameNumber = NULL, " +
            "LastChangedDate = @LastChangedDate WHERE PersonalIdentityNumber = @PersonalIdentityNumber";



            command1 = new SqlCommand(query, connection);
            command1.Parameters.Clear();
            return command1;
        }

         if ((!string.IsNullOrEmpty(s)) && !string.IsNullOrEmpty(t))
        {
            query += ", UnregistrationReason = @UnregistrationReason";
            query += ", GivenNameNumber = @GivenNameNumber ";

            command1 = new SqlCommand(query, connection);

            command1.Parameters.AddWithValue("@UnregistrationReason", s ?? DBNull.Value.ToString());
            command1.Parameters.AddWithValue("@GivenNameNumber", t ?? DBNull.Value.ToString());

        }
        else if (!string.IsNullOrEmpty(s) && string.IsNullOrEmpty(t))
        {
            query += ", UnregistrationReason = @UnregistrationReason, GivenNameNumber = @GivenNameNumber WHERE PersonalIdentityNumber = @PersonalIdentityNumber";
            command1 = new SqlCommand(query, connection);

            t = "00";
            command1.Parameters.AddWithValue("@UnregistrationReason", s ?? DBNull.Value.ToString());
            command1.Parameters.AddWithValue("@GivenNameNumber", t ?? DBNull.Value.ToString());

        }

        else if (string.IsNullOrEmpty(s) && !string.IsNullOrEmpty(t))
        {
            query += ", GivenNameNumber = @GivenNameNumber WHERE PersonalIdentityNumber = @PersonalIdentityNumber";
            command1 = new SqlCommand(query, connection);

            command1.Parameters.AddWithValue("@GivenNameNumber", t ?? DBNull.Value.ToString());

        }
        else
        {

                query += ", GivenNameNumber = @GivenNameNumber WHERE PersonalIdentityNumber = @PersonalIdentityNumber";
                t = "00";
                command1 = new SqlCommand(query, connection);

                command1.Parameters.AddWithValue("@GivenNameNumber", t ?? DBNull.Value.ToString());

                return command1;

        }
         return command1;

    }


    catch(Exception e)
{
    throw;
}

    }

1 个答案:

答案 0 :(得分:2)

AvReg方法中的逻辑是有缺陷的。第一个model = Sequential([ TimeDistributed(Dense(8, input_dim=nb_features, Activation='softmax')), LSTM(4, dropout_W=0.2, dropout_U=0.2), Dense(1), Activation('sigmoid') ]) 创建SqlCommand,但第二个重新创建sqlcommand,删除第一个if中添加的参数(尽管这应该在@CitizenshipDate参数上给出错误。)

无论如何,在输入ifs之前创建命令并在离开方法之前设置命令文本

if

说你的问题评论中指出了其他错误,(如Gravell先生告诉你的DbNull.Value.ToString())我希望将所有参数创建移到for的外面在循环内循环我只会改变值。

public static SqlCommand Avreg(string s, string t, string p, string c, string d, SqlConnection connection)
{
    try
    {
        var query = "UPDATE ...."
        SqlCommand command1 = new SqlCommand();



        if (c == "0")
        {
            query += ", CitizenshipDate = null";
        }
        else
        {
            query += ", CitizenshipDate = @CitizenshipDate";
            command1.Parameters.AddWithValue("@CitizenshipDate", string.IsNullOrEmpty(c) ? DBNull.Value : (object)c);
        }

        if (d == "0")
        {
            query += ", NationalRegistrationDate = null;";
        }
        else
        {
            query += ", NationalRegistrationDate = @NationalRegistrationDate";
            DateTime myDate = DateTime.ParseExact(d, "yyyyMMdd",
            System.Globalization.CultureInfo.InvariantCulture);
            command1.Parameters.AddWithValue("@NationalRegistrationDate", myDate);
        }
        command1.CommandText = query;
        command1.Connection = connection;
    }
    return command1;
}

现在Avreg不应该创建SqlCommand并返回它,mothod只使用作为参数传递的命令,并且只有在SqlCommand中没有alredy时才添加所需的参数

SqlCommand command1 = new SqlCommand();
command1.Parameters.Add("@PersonalIdentityNumber", SqlDbType.NVarChar);
... add other parameters with the exact datatype here
foreach (Person p in myPersons)
{
    SqlCommand command1 = new SqlCommand();
    try
    {
        // Pass the command created above, not recreate another one...
        Avreg(command1, p.UnregistrationReason, 
                        p.GivenNameNumber, p.ProtectedIdentity, 
                        p.CitizenshipDate, p.NationalRegistrationDate);

        command1.Parameters["@PersonalIdentityNumber"].Value = string.Format("{0}{1}", p.PersonalIdentityNumber, p.SpecialIdentityNumber));
        .... set the values for the other parameters .....
        command1.ExecuteNonQuery();
    }
    catch(.....)
    ...
}