C#datatable错误'列不属于表' [英] C# datatable error 'column does not belong to table'

查看:1674
本文介绍了C#datatable错误'列不属于表'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用以下代码将数据从逗号分隔文件写入SQL Server表并运行到以下错误:列'Id'不属于表。



  private   string  writetotbl(IList< string>记录)
{
string connString = ConfigurationManager.ConnectionStrings [ myDBConnString]。ConnectionString;

尝试
{
var lkup = < span class =code-sdkkeyword>来自记录 记录
rec = records.Split(' ,'
选择 new 查询
{
Id = rec [ 0 ],
代码= rec [ 1 ],
说明= rec [ 2 ]
};

DataTable dt = new DataTable();
dt.Columns.Add( new DataColumn( @Id typeof int )));
dt.Columns.Add( new DataColumn( @Code typeof string )));
dt.Columns.Add( new DataColumn( @Description typeof string )));
DataRow dr = dt.NewRow();

foreach var i in lkup)
{
dr = dt.NewRow();
dr [ Id] = i.Id.Replace( \ );
dr [ 代码] = i.Code.Replace( \ );
dr [ 描述] = i.Description.Replace( \ );
dt.Rows.Add(dr);
}

使用 var conn = new SqlConnection(connString))
{
conn .Open();
使用(SqlBulkCopy s = new SqlBulkCopy(conn))
{
s.DestinationTableName = 查找;
s.BatchSize = dt.Rows.Count;
s.BulkCopyTimeout = 0 ;
s.ColumnMappings.Add( Id ID);
s.ColumnMappings.Add( Code 代码);
s.ColumnMappings.Add( Description 描述);
s.WriteToServer(dt);
s.Close();
}
conn.Close();
}
return null );
}
catch (例外情况)
{
errmsg = ex.Message;
return (errmsg);
}
}





不确定是什么原因引起的。



提前谢谢..



我尝试了什么:



你能不能帮我提问。我拥有文件中的所有列,datatable与数据库表中的列名匹配,并具有适当的数据类型。不知道如何弹出这个错误。

解决方案

简化你的代码,然后再试一次:

 DataTable dt =  new  DataTable(); 
dt.Columns.Add( new DataColumn( @Id typeof int )));
DataRow dr = dt.NewRow();
dr [ Id] = 666 ;

你会得到同样的错误。

错误是对的:列'Id'不属于表

列'@Id'确实如此:

 DataTable dt =  new  DataTable(); 
dt.Columns.Add( new DataColumn( @Id typeof int )));
DataRow dr = dt.NewRow();
dr [ @ Id] = 666 ;

工作正常。



因此,通过更改初始名称来匹配您的列:

 DataTable dt =  new  DataTable(); 
dt.Columns.Add( new DataColumn( Id typeof int )));
dt.Columns.Add( new DataColumn( 代码 typeof string )));
dt.Columns.Add( new DataColumn( 说明 typeof string )));
DataRow dr = dt.NewRow();

foreach var i in lkup)
{
dr = dt.NewRow();
dr [ Id] = i.Id.Replace( \ );
dr [ 代码] = i.Code.Replace( \ );
dr [ 描述] = i.Description.Replace( \ );
dt.Rows.Add(dr);
}

或更改用法:

< pre lang =c#> DataTable dt = new DataTable();
dt.Columns.Add( new DataColumn( @ Id typeof int )));
dt.Columns.Add( new DataColumn ( @ Code typeof string )));
dt.Columns.Add( new DataColumn( @ Description typeof string )));
DataRow dr = dt.NewRow();

foreach var i in lkup)
{
dr = dt.NewRow();
dr [ @ Id] = i.Id.Replace( \ );
dr [ @ Code ] = i.Code.Replace( \ );
dr [ @ Description] = i.Description.Replace( \ );
dt.Rows.Add(dr);
}


Hi, I am using the below code to write data from a comma separated file into a SQL Server table and run into the following Error: Column 'Id' does not belong to table.

private string writetotbl(IList<string> records)
{
    string connString = ConfigurationManager.ConnectionStrings["myDBConnString"].ConnectionString;

    try
    {
        var lkup = from record in records
                         let rec = records.Split(',')
                         select new Lookup
                         {
                             Id = rec[0],
                             Code = rec[1],
                             Description = rec[2]
                         };

        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("@Id", typeof(int)));
        dt.Columns.Add(new DataColumn("@Code", typeof(string)));
        dt.Columns.Add(new DataColumn("@Description", typeof(string)));
        DataRow dr = dt.NewRow();

        foreach (var i in lkup)
        {
            dr = dt.NewRow();
            dr["Id"] = i.Id.Replace("\"", "");
            dr["Code"] = i.Code.Replace("\"", "");
            dr["Description"] = i.Description.Replace("\"", "");
            dt.Rows.Add(dr);
        }

        using (var conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlBulkCopy s = new SqlBulkCopy(conn))
            {
                s.DestinationTableName = "Lookup";
                s.BatchSize = dt.Rows.Count;
                s.BulkCopyTimeout = 0;
                s.ColumnMappings.Add("Id", "Id");
                s.ColumnMappings.Add("Code", "Code");
                s.ColumnMappings.Add("Description", "Description");
                s.WriteToServer(dt);
                s.Close();
            }
            conn.Close();
        }
        return (null);
    }
    catch (Exception ex)
    {
        errmsg = ex.Message;
        return (errmsg);
    }
}



Not sure what might be causing this.

Thank you in advance..

What I have tried:

Could you please help me with ideas. I have all the columns in the file, datatable match the column name in the database table and with appropriate datatype. Not sure how this error pops up.

解决方案

Simplify your code, and try again with that:

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("@Id", typeof(int)));
DataRow dr = dt.NewRow();
dr["Id"] = 666;

You will get the same error.
And the error is right: "Column 'Id' does not belong to table"
Column '@Id' does though:

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("@Id", typeof(int)));
DataRow dr = dt.NewRow();
dr["@Id"] = 666;

Will work fine.

So match your columns up, either by changing the initial names:

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(int)));
dt.Columns.Add(new DataColumn("Code", typeof(string)));
dt.Columns.Add(new DataColumn("Description", typeof(string)));
DataRow dr = dt.NewRow();

foreach (var i in lkup)
{
    dr = dt.NewRow();
    dr["Id"] = i.Id.Replace("\"", "");
    dr["Code"] = i.Code.Replace("\"", "");
    dr["Description"] = i.Description.Replace("\"", "");
    dt.Rows.Add(dr);
}

Or changing the usage:

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("@Id", typeof(int)));
dt.Columns.Add(new DataColumn("@Code", typeof(string)));
dt.Columns.Add(new DataColumn("@Description", typeof(string)));
DataRow dr = dt.NewRow();

foreach (var i in lkup)
{
    dr = dt.NewRow();
    dr["@Id"] = i.Id.Replace("\"", "");
    dr["@Code"] = i.Code.Replace("\"", "");
    dr["@Description"] = i.Description.Replace("\"", "");
    dt.Rows.Add(dr);
}


这篇关于C#datatable错误'列不属于表'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆