【问题标题】:SQL Server database update query in WinformsWinforms 中的 SQL Server 数据库更新查询
【发布时间】:2018-12-23 07:11:30
【问题描述】:

enter image description here我正在编写更新查询,但它不起作用。当我运行此代码时,我的数据库表中没有任何更新。 我的连接字符串是

public partial class cutomers : Form
{
    public static string connection = 
         @"Data Source=HOME-PC\SQLEXPRESS;Initial Catalog=WATER-Supply;Integrated Security=True";
    SqlConnection con = new SqlConnection(connection);

    private void update_Click(object sender, EventArgs e)
    {
        con.Open();

        string UPDATE = "UPDATE Customer_db SET Cust_Phone = @cusphone, Cust_Email = @cusemail, Cust_Address = @cusaddress WHERE Cust_Name = @cusname";

        SqlCommand cmd = new SqlCommand(UPDATE, con);

        cmd.Parameters.AddWithValue("@cusname", cusname.Text);
        cmd.Parameters.AddWithValue("@cusphone", cusphone.Text);
        cmd.Parameters.AddWithValue("@cusemail", cusemail.Text);
        cmd.Parameters.AddWithValue("@cusaddress", cusaddress.Text);

        int i = cmd.ExecuteNonQuery();
        con.Close();

        MessageBox.Show("Customer Update...");
    }
}

【问题讨论】:

  • 您能告诉我们您的连接字符串吗?
  • 我已经在顶部使用我的数据库表连接,请参阅公共部分类客户:Form { public static string connection = @"Data Source=HOME-PC\SQLEXPRESS;Initial Catalog=WATER-Supply;综合安全=真"; SqlConnection con = new SqlConnection(connection);
  • 不要将代码示例或示例数据放入 cmets - 因为您无法对其进行格式化,所以阅读它非常困难....而是:更新您的问题,编辑它以提供附加信息!谢谢。
  • MessageBox 不会做任何事情(充其量)并且Form 不是 ASP.Net 的一部分...您能否检查一下您添加到帖子中的标签?
  • 您确定您正在查看正确的数据库来检查值...

标签: c# sql-server winforms


【解决方案1】:

我创建了一个控制台应用程序,并且能够使用以下代码进行更新。 command.ExecuteNonQuery()返回的值是多少?

class Program
{
    static void Main(string[] args)
    {
        UpdateCustomerCommand(Guid.Parse("77ceef70-ab98-4392-835d-66c9face5f16"), "John Doe", "johndoe@acme.com");
    }

    public static string connectionString = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=labdb;Integrated Security=True;Pooling=False";

    private static void UpdateCustomerCommand(Guid Id, string name, string email)
    {
        var updateCommand = "UPDATE [Customer] SET [Name] = @NAME, [EMAIL] = @EMAIL WHERE [Id] = @ID";
        using (SqlConnection connection = new SqlConnection(
                   connectionString))
        {
            SqlCommand command = new SqlCommand(updateCommand, connection);
            command.Parameters.Add("@ID", SqlDbType.UniqueIdentifier);
            command.Parameters["@ID"].Value = Id;
            command.Parameters.Add("@NAME", SqlDbType.NVarChar, 150);
            command.Parameters["@NAME"].Value = name;
            command.Parameters.Add("@EMAIL", SqlDbType.NVarChar, 100);
            command.Parameters["@EMAIL"].Value = email;
            command.Connection.Open();
            command.ExecuteNonQuery();
        }
    }

【讨论】:

  • 谢谢先生,我会试试这个代码,然后我会回复你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-06
  • 1970-01-01
相关资源
最近更新 更多