c# - 在没有用户输入的情况下将 View 中的信息插入到另一个表中

标签 c# sql visual-studio-2010

我正在尝试使用 C#Visual Studio 2010 创建一个 Web 服务,它将使用 SQL 将 View 表中的值插入到另一个表中查询。过去我们使用过这种类型的服务,但我们要求用户输入他们想要更新的值,但是对于我正在使用的查询,不需要用户输入,因为只有在其中一个时才会插入值字段值为 NULL

下面是我一直在尝试使用的代码,我改编了我们之前使用的代码,但在这里我仍然期待用户输入。我怎样才能更新我的代码而不是期望输入。我知道我可以从 MySQL 运行这个查询,但我希望能够在我们的站点中创建一个按钮,以便其他人可以这样做。这是运行查询的代码:

public void AddUsers(string json)
{
    GetDbInstance();

    var sql = "insert into dbo.ASPECTS_users_activity " +
              "(user_id, usr_address, usr_phone, usr_email, usr_website, usr_photo ) " +
              "select @v.customer_id, @usr_address, @usr_phone, @usr_email, @usr_website, @usr_photo  " +
              "from dbo.ASPECTS_current_users_vw v where usr_photo is NULL;";

    var usrInformation = JsonConvert.DeserializeObject<UpdateExhibitors>(json);
    Console.WriteLine(usrInformation);

    var conn = db.Connection();

    try
    {
        SqlCommand command = new SqlCommand(sql, conn);

        command.Parameters.AddWithValue("@v.customer_id", usrInformation.master_customer_id);
        command.Parameters.AddWithValue("@usr_address", "123 XYZ Post, Columbus, IN");
        command.Parameters.AddWithValue("@usr_phone", "555-555-5555");
        command.Parameters.AddWithValue("@usr_email", "test@sample.com");
        command.Parameters.AddWithValue("@usr_website", "http://www.sample.com");
        command.Parameters.AddWithValue("@usr_photo", "");

        command.ExecuteNonQuery();

        command.Dispose();

        command = null;

    }
    catch (Exception e)
    {
        throw new Exception(e.ToString(), e);
    }
    finally
    {
        conn.Close();
    }
}

这是模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Customer_Insert.Models {
    public class AddUsers {
        public string customer_id { get; set; }
        public string usr_address { get; set; }
        public string usr_phone { get; set; }
        public string usr_email { get; set; }
        public string usr_website { get; set; }
        public string usr_photo { get; set; }
    }
}

只是让您知道唯一将从 View 填充到的字段是 customer_id 字段,所有其他字段都将具有默认值,这些值将在另一点更新。这里知道 SQL 的人不多,因此如果我不在身边,创建此选项将提供一个选项。

最佳答案

因为您“可以从 MySQL 运行这个查询”,我会将该查询作为一个存储过程保存在您的数据库中。那么您的代码中既不需要 SQL 也不需要参数。

SqlCommand command = new SqlCommand("yourStoredProcedureName", conn);
command.ExecuteNonQuery();
...

关于c# - 在没有用户输入的情况下将 View 中的信息插入到另一个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31455296/

相关文章:

c# - C#.Net 中的连接字符串

c# - 从另一个数组中删除一个数组的高效算法

c# - 将其转换为 SQL 的最佳方法是什么

对于其中一个表,不同列上的 2 个表之间的 MySQL 多重内部连接

c++ - 如何在Visual Studio中链接库

c# - 我怎样才能学会 DirectShow 编程?

java - 写入数据库时​​出现异常(org.hibernate.exception.ConstraintViolationException)

sql - Postgresql 复合唯一键(姓氏、名字、中间名)不起作用?

visual-studio-2010 - Visual Studio 单击“查找结果”在错误的窗口中打开代码

c++ - 如何在 VS 项目中构建和链接 libCurl