简体   繁体   English

C#插入语句-数组作为SQL列名

[英]c# Insert statement - array as sql column name

I have two tables, Vehicle_Status and Trans_Income . 我有两个表Vehicle_StatusTrans_Income

Vehicle_Status contain two columns, Vehicle_Number and Status , and the Vehicle_Number consists the data which are the column name of Trans_Income . Vehicle_Status包含两列Vehicle_NumberStatus ,而Vehicle_Number包含数据,它们是Trans_Income的列名。

I want to load the data from Vehicle_Number into an array and use it in the insert statement. 我想将Vehicle_Number的数据加载到数组中并在insert语句中使用它。

string[] vehicleActive = new string[100];

SqlCommand cmd = new SqlCommand("Select Vehicle_Number from [dbo].[Vehicle_Status] Where Status = 'Inactive'", con);
cmd.CommandType = CommandType.Text;

con.Open();
int a = 0;

SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{
    vehicleActive[a] = dr["Vehicle_Number"].ToString();
    a++;
}

con.close();

for (int b = 0; b < a; b++)
{
    cmd = new SqlCommand("Insert Into [dbo].[Trans_Income](Month, Particular,'"+vehicleActive[b]+"') VALUES (@Month, @Particular, @vehicleNo )", con);

    con.Open();

    cmd.Parameters.AddWithValue("@Month", textBox2.Text);
    cmd.Parameters.AddWithValue("@Particular", textBox2.Text);
    cmd.Parameters.AddWithValue("@vehicleNo", textBox3.Text);

    cmd.ExecuteNonQuery();
    con.Close();    
}   

But I get the error 但是我得到了错误

System.Data.SqlClient.SqlException: 'Incorrect syntax near 'BJW6719'.' System.Data.SqlClient.SqlException:'BJW6719'附近的语法不正确。

One of the column names is BJW6719 - can anyone help me with this? 列名称之一是BJW6719有人可以帮我吗? Or is there any other, better way for doing this? 还是有其他更好的方法呢? Thank you in advance. 先感谢您。

I notice you have extra ' character around your column name, they are not required. 我注意到您的列名周围有多余的'字符,这不是必需的。 Try with the below code: 尝试以下代码:

cmd = new SqlCommand("Insert Into [dbo].[Trans_Income](Month, Particular,"+vehicleActive[b]+") VALUES (@Month, @Particular, @vehicleNo )", con);

Are you sure that your third column name is: BJW6719 . 您确定您的第三列名称是: BJW6719 If yes the above will work otherwise you need to add the correct column name. 如果是,则上述方法将起作用,否则,您需要添加正确的列名称。

if you are using .net framework 4.5 up then you can simply convert table into arrey here is code 如果您使用的是.net Framework 4.5以上版本,则只需将表转换为Arrey,这是代码

try
                {
                  var dt = new DataTable();
                  var cmd = new SqlCommand();
                  cmd.CommandText = @"Select Vehicle_Number from [dbo].[Vehicle_Status] Where Status = 'Inactive'";
                                   var da = new SqlDataAdapter(cmd);
                  da.Fill(dt);
                }
             catch (Exception ex)
                {

                }
             finally
                {
                  if (cmd.Connection.State == ConnectionState.Open)
                   {
                     cmd.Connection.Close();
                   }
                }

               var vehicleActive= dt.Rows[0].ItemArray.Select(x => x.ToString()).ToArray();

then you can loop through your array like this 那么你可以像这样遍历你的数组

for (int i = 0; i < vehicleActive.Length; i++)
                    {
                        cmd = new SqlCommand("Insert Into [dbo].[Trans_Income](Month, Particular,'"+vehicleActive[b]+"') VALUES (@Month, @Particular, @vehicleNo )", con);

                        con.Open();


                        cmd.Parameters.AddWithValue("@Month", textBox2.Text);
                        cmd.Parameters.AddWithValue("@Particular", textBox2.Text);
                        cmd.Parameters.AddWithValue("@vehicleNo", textBox3.Text);

                        cmd.ExecuteNonQuery();
                        con.Close();    
                     }  

this is better way to loop through database from code and there might be issue with column name so check column name from sql server before defining in sql query from code 这是从代码循环遍历数据库的更好方法,列名称可能存在问题,因此在从代码中的sql查询中定义之前,请先从sql server中检查列名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM