【问题标题】:SQL Database not 'saving' information added through Code (C#)SQL 数据库未“保存”通过代码 (C#) 添加的信息
【发布时间】:2020-07-20 15:07:30
【问题描述】:

我正在使用 Dapper 将我的代码连接到我的 LocalDB。我有很多其他功能可以完美地访问信息并可以读取它并将其带入我的程序,但由于某种原因,我将信息插入数据库的功能没有做任何事情。我已经确认没有抛出异常,并且在单击按钮时到达代码(如消息框所示),但没有任何数据保存到数据库中。我还检查了我的查询,当我直接查询数据库时它工作正常,只是当我让代码出于某种原因执行它时(因此我的问题)。出了什么问题?

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

namespace ProjectManager
{
class Database
{
    // Connection Variables
    private IDbConnection connection = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Database"].ConnectionString);

    // Get Number of Projects
    public int GetNumOfProjects()
    {
        return connection.Query<int>("select count(*) from Projects").FirstOrDefault();
    }
    // Load Projects
    public ProjectPane LoadProjects(int i, Point start)
    {
        // Variables
        string title;
        string subject;
        int progress;

        // Load ProjectPane with Information
        title = connection.Query<string>("select title from (select Row_Number() Over (Order by progress desc) as RowNum, *From Projects) t2 Where RowNum = " + i.ToString()).FirstOrDefault();
        subject = connection.Query<string>("select subject from (select Row_Number() Over (Order by progress desc) as RowNum, *From Projects) t2 Where RowNum = " + i.ToString()).FirstOrDefault();
        progress = connection.Query<int>("select progress from (select Row_Number() Over (Order by progress desc) as RowNum, *From Projects) t2 Where RowNum = " + i.ToString()).FirstOrDefault();

        ProjectPane p = new ProjectPane(title, subject, progress);
        p.Location = start;
        p.LoadData();
        return p;
    }
    // Create Project Entry
    public void CreateProject(string title, string subject, int progress)
    {
        try
        {
            connection.Query($"insert into Projects (title, subject, progress) values ('Hello', 'There', 50)");
            MessageBox.Show("Project Created");
        }
        catch (SqlException ex) 
        {
            MessageBox.Show(ex.ToString());
        }
        
    }
    // Edit Project

    // Complete Projects

    // Delete Project
}

}

【问题讨论】:

  • 除此之外你应该使用 Execute 而不是 Query 你是如何检查“它什么都不做”

标签: c# .net sql-server dapper


【解决方案1】:

使用执行而不是查询

source

string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
    var affectedRows = connection.Execute(sql, new {CustomerName = "Mark"});

    Console.WriteLine(affectedRows);

    var customer = connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerName = 'Mark'").ToList();

    FiddleHelper.WriteTable(customer);
}

【讨论】:

    【解决方案2】:

    您的描述在我看来就像您的数据操作应该刷新到数据库或以其他方式提交。也许您可以定义您的连接以启用自动提交。 问候。

    【讨论】:

      猜你喜欢
      • 2020-10-06
      • 2017-03-02
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多