-4

新手来了 需要一些建议。我想将值插入数据库,到目前为止代码中没有错误。我无法弄清楚为什么它无法将值插入数据库的问题

这是我的代码

            string Constr = @"Data Source=(local);Integrated Security=True;Database=DDLYBank";
            SqlConnection Conn = new SqlConnection(Constr);
            string MainAccInfo = "Select AccountNo, HashValue from MainAcc Where HashValue = '" + ccinfo + "'";
            SqlCommand cmd1 = new SqlCommand(MainAccInfo, Conn);
            SqlDataReader read1 = cmd1.ExecuteReader();


            using (SqlCommand command = Conn.CreateCommand())
            {
                command.CommandText = "Insert INTO Record (AccountNo, HashValue, Debit, Balance, Date_Time, FingerPrint) Values (@AccountNo, @HashValue, @Debit, @Balance, @Date_Time, @FingerPrint)";

                command.Parameters.AddWithValue("@AccountNo", read1["AccountNo"]);
                command.Parameters.AddWithValue("@HashValue", read1["HashValue"]);
                command.Parameters.AddWithValue("@Debit", rDebit);
                command.Parameters.AddWithValue("@Balance", aBalance);
                command.Parameters.AddWithValue("@Date_Time", rDate);
                command.Parameters.AddWithValue("@FingerPrint", rFingerPrint);

                Conn.Open();
                command.ExecuteNonQuery();
            }

        }
        catch (SqlException ex)
        {
            return;
        }
4

1 回答 1

1

首先,您尝试做的事情不应该像以前那样做 :) 使用 SQL Server 中的存储过程执行此操作。

CREATE PROC CreateNewFromHashValue
   @HashValue NVARCHAR(128),
   @Debit INT,
   @Balance INT,
   @Date DATETIME,
   @FingerPrint NVARCHAR(128)
AS

INSERT INTO Record (AccountNo, HashValue, Debit, Balance, Date_Time, FingerPrint)
SELECT AccountNo, HashValue,@Debit,@Balance,@Date,@FingerPrint
    FROM MainAcc 
     WHERE HashValue = @HashValue

SELECT TOP 1 *
    FROM Record
    ORDER BY DESC

GO 

然后我们从 .NET 调用它:

        // create and open a connection object
        SqlConnection conn = new SqlConnection("Data Source=(local);Integrated Security=True;Database=DDLYBank");
        try {
        conn.Open();

        // 1. create a command object identifying
        // the stored procedure
        SqlCommand cmd  = new SqlCommand("CreateNewFromHashValue", conn);

        // 2. set the command object so it knows
        // to execute a stored procedure
        cmd.CommandType = CommandType.StoredProcedure;

        // 3. add parameter to command, which
        // will be passed to the stored procedure
        cmd.Parameters.Add(new SqlParameter("@HashValue","YOUR HashValue"));
        cmd.Parameters.Add(new SqlParameter("@Debit",1)); // Your Debit,INT?
        cmd.Parameters.Add(new SqlParameter("@Balance",1)); // Your Balance ,INT?
        cmd.Parameters.Add(new SqlParameter("@Date",DateTime.Now)); // datetime :)
        cmd.Parameters.Add(new SqlParameter("@FingerPrint","FingerPrint")); // FingerPrint

        using (SqlCommand cmd = conn.CreateCommand())
        {
            cmd.ExecuteNonQuery();
        }

    } catch (Exception ex) {
        // NOT ONLY RETURN  HER !!! 
        Console.WriteLine(ex.Message.ToString());
    }


    //Remember to to close conn then you are done.
    conn.Close();
于 2013-07-05T08:48:48.417 回答