简体   繁体   English

无法使用C#更新我的SQL数据库

[英]Cant update my SQL database with c#

I am working on a database with C# and searching in the database is no problem, i get the correct information when searching. 我正在使用C#在数据库上工作,在数据库中搜索是没有问题的,在搜索时我会获得正确的信息。 Thou if i want to update my database with new Tigers (for an example) it wount update. 如果我想用新的Tigers更新我的数据库(例如),它将无法更新。 My self cant see any problems with the code and i do not get any errors. 我自己看不到代码有任何问题,我也没有收到任何错误。 Where can the problem be? 问题出在哪里? This is the code: 这是代码:

    public void UpdateTigers(string[] array, string persNo)
    {
        //Creating the connection string.
        string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Owner.mdf;Integrated Security=True";
        //Creating a connection to the database with the connection string value.
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            //Opening the connection to the SQL database
            con.Open();
            //Creating a commands(querys) to be used.
            SqlCommand cmd = new SqlCommand("UPDATE Owner SET Tiger1=@tiger1 WHERE Personnummer=" + persNo, con);
            if (array[6] != "")
            {
                cmd = new SqlCommand("UPDATE Owner SET Tiger1=@tiger1, Tiger2=@tiger2 WHERE Personnummer=" + persNo, con);
            }
            if (array[7] != "")
            {
                cmd = new SqlCommand("UPDATE Owner SET Tiger1=@tiger1, Tiger2=@tiger2, Tiger3=@tiger3 WHERE Personnummer=" + persNo, con);
            }
            //Adding to the SqlCommand what @Name got for value.
            cmd.Parameters.AddWithValue("@tiger1", array[6]);
            if (array[6] != "")
                cmd.Parameters.AddWithValue("@tiger2", array[7]);
            if (array[7] != "")
                cmd.Parameters.AddWithValue("@tiger3", array[8]);
            //Sending the request to the database.
            cmd.ExecuteNonQuery();

            //No need for a con.Close() because the using statement closes it automatically.
        }

Do not cram all the CRUD in one method. 不要用一种方法填充所有CRUD。 create a method for each one and return true if the query ran successfully and false if otherwise. 为每个查询创建一个方法,如果查询成功运行,则返回true,否则返回false。 Example: 例:

Use try catch. 使用try catch。 if there is no exception, then the query was ran successfully, then return true if sucess, return false if it isnt example 如果没有异常,则查询已成功运行,如果成功,则返回true;如果不是,则返回false

public static bool UpdateTiger(Tiger p)
{
    var command = new SqlCommand();
    command.CommandText = "UPDATE Owner SET Tiger1=@tiger1 WHERE Personnummer=@perNum";

    command.Parameters.AddWithValue("@tiger1", p.tiger1).Direction = ParameterDirection.Input;
    command.Parameters.AddWithValue("@pernum", p.pernum).Direction = ParameterDirection.Input;

    try
    {
        SqlHelper.ExecuteNonQuery(command); // this is where I run the procedure
        return true;
    }
    catch (Exception e)
    {

        return false;
    }

then create another method for other kinds of updates 然后为其他类型的更新创建另一种方法

I found the problem. 我发现了问题。 I had forgotten to update my array before sending it in to the code :). 我忘了在将数组发送到代码之前更新数组:)。 Thanks for your time! 谢谢你的时间!

Is persNo an int being passed as a string? persNo是否将int作为字符串传递? If it is an actual string, you might want to check the query. 如果它是实际字符串,则可能要检查查询。 If a string, your query would look like this: ...WHERE Personnummer=someString when you would really want ...WHERE Personnummer='someString' . 如果是字符串,则查询将如下所示: ...WHERE Personnummer=someString当您真正想要...WHERE Personnummer='someString' If that is not it, try putting a break in the code in Visual Studio on the line after cmd is set or Debug.Print(cmd.CommandText); 如果不是,请尝试在设置了cmdDebug.Print(cmd.CommandText);之后的行中在Visual Studio中的代码中插入一个中断Debug.Print(cmd.CommandText); somewhere after it is set to see what the query is that is being executed. 设置后的某处可以查看正在执行的查询。 Given there is no error, I would say your WHERE condition is not providing a match to update anything in the database. 鉴于没有错误,我想说您的WHERE条件没有提供匹配项来更新数据库中的任何内容。

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

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