C# - 连接到数据库并使用 SQL 服务器存储过程插入数据

C# - Connecting to database and inserting data with SQL Server stored procedure

我正在尝试自学一些 C#(在 90 年代后期曾用 C++ 编写代码,如果这能告诉您任何事情的话)。所以无论如何,我在 Visual Studio 2017 年尝试测试我的一个 classes 时一直处于测试失败状态。

设置如下:

在我的 ErrorLog.cs 文件中:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Configuration;

namespace ManagementLibrary {
    public class ErrorLog {
        private int log_id { get; set; }
        private string logException {get; set;}
        private string error { get; set; }
        private string date_time { get; set; }
        private string logObject { get; set; }

        public ErrorLog(string logException, string error, string date_time, string logObject) {
            this.logException = logException;
            this.error = error;
            this.date_time = date_time;
            this.logObject = logObject;
        }

        public int LogError() {
            using (SqlConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("DataConnection"))) {

                SqlCommand command = new SqlCommand("InsertErrorLog", connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlParameter p1 = new SqlParameter();
                p1.ParameterName = "@Exception";
                p1.SqlDbType = SqlDbType.VarChar;
                p1.Direction = ParameterDirection.Input;
                p1.Value = this.logException;

                SqlParameter p2 = new SqlParameter();
                p2.ParameterName = "@Error";
                p2.SqlDbType = SqlDbType.VarChar;
                p2.Direction = ParameterDirection.Input;
                p2.Value = this.error;

                SqlParameter p3 = new SqlParameter();
                p3.ParameterName = "@DateTime";
                p3.SqlDbType = SqlDbType.VarChar;
                p3.Direction = ParameterDirection.Input;
                p3.Value = this.date_time;

                SqlParameter p4 = new SqlParameter();
                p4.ParameterName = "@logObject";
                p4.SqlDbType = SqlDbType.VarChar;
                p4.Direction = ParameterDirection.Input;
                p4.Value = this.logObject;

                command.Parameters.Add(p1);
                command.Parameters.Add(p2);
                command.Parameters.Add(p3);
                command.Parameters.Add(p4);

                connection.Open();
                var identity = command.ExecuteNonQuery();
                return identity;
            }

        }

    }
}

本质上,我正在尝试为遇到错误时创建审计跟踪,它只是向数据库中添加一条记录。因此,我不仅尝试自学一些 C#,而且尝试学习如何在 Visual Studio 中进行单元测试。我已经按照一些关于设置和执行单元测试的教程进行操作,但是当我这样做时,我不断遇到以下故障:

Message: Test method ManagementLibrary.Tests.ErrorLogTests.LogErrorTest threw exception: System.NullReferenceException: Object reference not set to an instance of an object.

当我 "debug" 失败时 "goes to" 我的 Helper.cs 文件。现在 class 只读取 App.config 文件和 returns 连接字符串。

namespace ManagementLibrary {
    public static class Helper {
        public static string CnnVal(string name) {
            return ConfigurationManager.ConnectionStrings[name].ConnectionString;
        }
    }
}

并显示错误:'Object reference not set to an instance of an object'。

我什至不知道这是什么意思

想法?

当您使用 ConfigurationManager 时,它会读取条目 assembly/project 的 app.config。因此,在这种情况下,当您 运行 进行单元测试时,条目是您的测试项目,它没有 app.config。因此,每当您尝试使用 ConfigurationManager.

时,都会 运行 出错