C# 在表中插入多个值(用C语言)#

C# 在表中插入多个值(用C语言)#,c#,C#,我在一个名为“糖蜜分析”的表中有98列,我需要使用我的c#桌面应用程序插入记录。 我的代码示例如下所示 string insert_sql = @"insert into molasses_analysis(mo_entry_date, mo_entry_time, mo_code, mo_brix, mo_pol, mo_purity, mo_crtd_by) " + " values(@entry_date, @entry_time, @mol_code, @brix, @p

我在一个名为“糖蜜分析”的表中有98列,我需要使用我的c#桌面应用程序插入记录。
我的代码示例如下所示

    string insert_sql = @"insert into molasses_analysis(mo_entry_date, mo_entry_time, mo_code, mo_brix, mo_pol, mo_purity, mo_crtd_by) " +
    " values(@entry_date, @entry_time, @mol_code, @brix, @pol, @purity, @crtd_by)";
    try
        {
         List<SqlParameter> param = new List<SqlParameter>();
         param.Add(new SqlParameter("@entry_date", entry_date));
         param.Add(new SqlParameter("@entry_time", entry_time));
         param.Add(new SqlParameter("@mol_code", mol_code));
         param.Add(new SqlParameter("@brix", brix));
         param.Add(new SqlParameter("@pol", pol));
         param.Add(new SqlParameter("@purity", purity));
         param.Add(new SqlParameter("@crtd_by", crtd_by));
        int inserted_rows = SqlHelper.ExecuteNonQuery(dbConn.sqlConn(),CommandType.Text, insert_sql, param.ToArray());
        }
catch (Exception ex)
      {
        MessageBox.Show("Data not saved!\nError message - "+ex.Message, "Error!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
string insert\u sql=@“插入糖蜜分析(mo\u输入日期、mo\u输入时间、mo\u代码、mo\u brix、mo\u pol、mo\u纯度、mo\u crtd\u by)”+
“值(@entry_date、@entry_time、@mol_code、@brix、@pol、@purity、@crtd_by)”;
尝试
{
列表参数=新列表();
参数Add(新的SqlParameter(“@entry_date”,entry_date));
参数Add(新的SqlParameter(“@entry_time”,entry_time));
参数Add(新的SqlParameter(“@mol_code”,mol_code));
参数Add(新的SqlParameter(“@brix”,brix));
参数Add(新的SqlParameter(“@pol”,pol));
参数Add(新的SqlParameter(“@pursity”,pursity));
参数Add(新的SqlParameter(“@crtd_by”,crtd_by));
int inserted_rows=SqlHelper.ExecuteNonQuery(dbConn.sqlConn(),CommandType.Text,insert_sql,param.ToArray());
}
捕获(例外情况除外)
{
MessageBox.Show(“数据未保存!\n错误消息-”+ex.message,“Error!!”,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
在这里,我只使用了七个字段/列,但是为98列编写这样的代码并为每列分配Sql参数将是非常繁忙和痛苦的。
我的问题是,有没有更干净、更好的代码可以使用c代码插入多列

简单的答案是否定的;与使用局部变量填充每个
SqlParameter
的方式不同

using System.Collections.Generic; // for dictionary
using System.Text; // for stringbuilder

// ...

// create a dictionary then use a literal to make it easier to populate
Dictionary<string, string> data = new Dictionary<string, string>
{
    { "entry_date", "SOMEVALUE1" }, 
    { "entry_time", "SOMEVALUE2" }
    // add more params and values here...
};

// start our query and params list
StringBuilder query = new StringBuilder("YOUR QUERY STARTS HERE");
List<SqlParameter> params = new List<SqlParameter>();

// iterate over each key/value pair, appending to the query and params list
foreach (KeyValuePair<string, string> pair in data) {
    query.Append("@" + pair.Key);
    params.Add(new SqlParameter(pair.Key, pair.Value));
}
一种解决方案是,如果这些局部变量中的每一个都存储在一个(键/值对)中,那么您可以使用一个并迭代字典键来构建SQL查询字符串。在同一循环中,您可以添加每个
SqlParameter

using System.Collections.Generic; // for dictionary
using System.Text; // for stringbuilder

// ...

// create a dictionary then use a literal to make it easier to populate
Dictionary<string, string> data = new Dictionary<string, string>
{
    { "entry_date", "SOMEVALUE1" }, 
    { "entry_time", "SOMEVALUE2" }
    // add more params and values here...
};

// start our query and params list
StringBuilder query = new StringBuilder("YOUR QUERY STARTS HERE");
List<SqlParameter> params = new List<SqlParameter>();

// iterate over each key/value pair, appending to the query and params list
foreach (KeyValuePair<string, string> pair in data) {
    query.Append("@" + pair.Key);
    params.Add(new SqlParameter(pair.Key, pair.Value));
}
使用System.Collections.Generic;//词典
使用System.Text;//对于stringbuilder
// ...
//创建一个字典,然后使用文字使其更易于填充
字典数据=新字典
{
{“输入日期”,“SOMEVALUE1”},
{“entry_time”,“SOMEVALUE2”}
//在此处添加更多参数和值。。。
};
//启动我们的查询和参数列表
StringBuilder查询=新建StringBuilder(“您的查询从这里开始”);
列表参数=新列表();
//迭代每个键/值对,并附加到查询和参数列表
foreach(数据中的KeyValuePair对){
query.Append(“@”+pair.Key);
Add(新的SqlParameter(pair.Key,pair.Value));
}

注意:以上代码是演示如何使用字典和StringBuilder的示例;应该仔细研究,而不是复制粘贴。

如果您的属性名称和列名称相同,这个答案将对您有所帮助。 首先,使用下面给出的SQL代码获取列名

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'molasses_analysis';
//this returns column names and column types
然后将表分配给包含列名的列表

List<string> listColNames = new List<string>(); 

不是真的,不。你可以通过列及其值的列表使整个事情变得动态,但是你仍然需要执行类似的东西。不,没有AFAIK。您应该研究EntityFramework或类似的库。通过为您管理所有这些内容,您可以节省大量时间。您可以做的“最好”是动态构建SQL语句,方法是创建一个字典或需要向其中添加值的列列表,以及一个类似的列表(或相同的字典/列表),其中包含需要放入这些列的值,然后使用循环组合所有这些内容,以连接正确的SQL并添加所有参数。