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/

相关文章:

sql - 查找连续序列并建议序列中的下一个数字

sql - 如何使用连接查询两个表并从一个表中获取所有行,并从另一个中获取相关行?

visual-studio - 使用 Visual Studio 2010 查看有关错误的完整 WCF 响应

c# - asp.net core 中的授权类型

c# - 数据表中的验证

c# - 获取当前时间为自 01.01.2001 00 :00 in c# 以来的分钟数

asp.net - Visual Studio 2010远程调试非常慢(跨域,通过VPN)

c# - C# 数组中的 System.InvalidCastException 错误

php - pg_query() : Query failed: ERROR: more than one row returned by a subquery used as an expression

visual-studio-2010 - Visual Studio 在 IE9 中调试 JavaScript 失败 - 说 "The correct version of pdm.dll is not registered"