You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SqlServerService未实现接口成员问题排查(C# WinForms)

问题解决:SqlServerService未实现接口成员iDatabase.CreateUser

错误原因分析

  • 命名空间不匹配:SqlServerService位于TravelCompany.app.repository命名空间,但iDatabase接口未显示所属命名空间,大概率与SqlServerService不在同一命名空间,且SqlServerService未引用接口所在命名空间,导致编译器无法识别接口的CreateUser方法与实现的方法属于同一契约。
  • 实现方法存在无效代码:CreateUser方法内部使用了未声明的变量(如full_name、email等),且方法末尾抛出NotImplementedException,即便接口实现问题解决,这些代码也会触发运行时错误。

具体修复步骤

1. 统一命名空间或添加引用

如果iDatabase接口与SqlServerService不在同一命名空间,在SqlServerService文件顶部添加接口所在命名空间的引用:

using 你的iDatabase接口所在命名空间; // 替换为实际命名空间

或者直接将iDatabase接口移至TravelCompany.app.repository命名空间下。

2. 修复CreateUser方法的变量与实现问题

通过方法参数传递用户数据是更合理的方式,需同步修改接口与实现类:

修改iDatabase接口(新增User实体类传递参数):

public interface iDatabase
{
    void createConnection();
    void CreateUser(User user);
}

// 定义用户实体类
public class User
{
    public string full_name { get; set; }
    public string email { get; set; }
    public string password { get; set; }
    public string cpf { get; set; }
    public DateTime birthday { get; set; }
    public string cep { get; set; }
    public string street { get; set; }
    public string neighborhood { get; set; }
    public string city { get; set; }
    public string uf { get; set; }
    public string complement { get; set; }
    public string passport_number { get; set; }
    public string stack { get; set; }
    public int xp { get; set; }
    public string seniority { get; set; }
}

修改SqlServerService的CreateUser方法:

public void CreateUser(User user)
{
    try
    {
        // 确保数据库连接已打开
        if (sqlCon == null || sqlCon.State != ConnectionState.Open)
        {
            createConnection();
        }
        
        strSql = "INSERT INTO users (full_name, email, password, cpf, birthday, cep, street, neighborhood, city, uf, complement, passport_number, stack, xp, seniority) VALUES (@full_name, @email, @password, @cpf, @birthday, @cep, @street, @neighborhood, @city, @uf, @complement, @passport_number, @stack, @xp, @seniority)";
        command = new SqlCommand(strSql, sqlCon);
        
        // 绑定实体类参数
        command.Parameters.AddWithValue("@full_name", user.full_name);
        command.Parameters.AddWithValue("@email", user.email);
        command.Parameters.AddWithValue("@password", user.password);
        command.Parameters.AddWithValue("@cpf", user.cpf);
        command.Parameters.AddWithValue("@birthday", user.birthday);
        command.Parameters.AddWithValue("@cep", user.cep);
        command.Parameters.AddWithValue("@street", user.street);
        command.Parameters.AddWithValue("@neighborhood", user.neighborhood);
        command.Parameters.AddWithValue("@city", user.city);
        command.Parameters.AddWithValue("@uf", user.uf);
        command.Parameters.AddWithValue("@complement", user.complement);
        command.Parameters.AddWithValue("@passport_number", user.passport_number);
        command.Parameters.AddWithValue("@stack", user.stack);
        command.Parameters.AddWithValue("@xp", user.xp);
        command.Parameters.AddWithValue("@seniority", user.seniority);
        
        command.ExecuteNonQuery();
        MessageBox.Show("User created successfully!");
    }
    catch (Exception e)
    {
        MessageBox.Show("Error to create user!", e.Message);
    }
    // 移除多余的NotImplementedException抛出语句
}

3. 更新DbService的insertUser方法

同步修改DbService中的方法,传递User参数:

public void insertUser(User user)
{
    this._database.CreateUser(user);
}

额外优化建议

  • 接口命名规范:遵循.NET命名规范,接口名称以大写I开头,改为IDatabase。
  • 连接资源管理:使用using语句自动释放SqlConnection和SqlCommand资源,避免连接泄漏:
public void CreateUser(User user)
{
    try
    {
        using (SqlConnection sqlCon = new SqlConnection(strCon))
        {
            sqlCon.Open();
            strSql = "INSERT INTO users (...) VALUES (...)";
            using (SqlCommand command = new SqlCommand(strSql, sqlCon))
            {
                // 绑定参数...
                command.ExecuteNonQuery();
                MessageBox.Show("User created successfully!");
            }
        }
    }
    catch (Exception e)
    {
        MessageBox.Show("Error to create user!", e.Message);
    }
}

内容的提问来源于stack exchange,提问作者dev user

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 02:31:15