将数据插入服务器.....?

时间:2012-01-11 07:17:26

标签: asp.net sql c#-4.0

我正在尝试阅读excel表的一些指定列,并使用日期,移动编号和一些列,我能够读取列,但是将值插入数据库时​​出现问题...... ..................

    public static DataTable columnNamessheet2(String excelFile)
    {
        string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFile + ";Extended Properties='Excel 12.0;'";
        string connectionstring = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DebitCare;Data Source=SSDEV7-HP\\SQLEXPRESS";

        using (OleDbConnection conn1 = new OleDbConnection(connString))
        {
            SqlConnection SqlConn1 = new SqlConnection(connectionstring);
            SqlConn1.Open();
            OleDbCommand odc1 = new OleDbCommand(string.Format("Select  LOAN_NO,DOB,DEALER_NAME,CUST_MOBILE_NO  FROM [ICICI TW DUMP$]"), conn1);
            conn1.Open();
            OleDbDataReader reader1 = odc1.ExecuteReader();
            int LOAN_NO = reader1.GetOrdinal("LOAN_NO");
            int DEALER_NAME = reader1.GetOrdinal("DEALER_NAME");
            int DOB = reader1.GetOrdinal("DOB");
            int CUST_MOBILE_NO = reader1.GetOrdinal("CUST_MOBILE_NO");
            while (reader1.Read())
            {
                string LnNumber = reader1.GetValue(LOAN_NO).ToString();
                string cname = reader1.GetValue(DEALER_NAME).ToString();
                string CDOB = reader1.GetValue(DOB).ToString();
                string CMobNo = reader1.GetValue(CUST_MOBILE_NO).ToString();
            }
            DataTable sheetSchema1 = reader1.GetSchemaTable();
            SqlBulkCopy sqlbulk = new SqlBulkCopy(SqlConn1);
            sqlbulk.DestinationTableName = "DebitCare";
            sqlbulk.ColumnMappings.Add("LnNumber", "CUSTOMER_LOAN_NO");
            sqlbulk.ColumnMappings.Add("cname", "CUSTOMER_NAME");
            sqlbulk.ColumnMappings.Add("CDOB", "DOB");
            sqlbulk.ColumnMappings.Add("CMobNo", "CUST_MOBILE_NO");
            sqlbulk.WriteToServer(reader1);
            return sheetSchema1;
        }

    }

1 个答案:

答案 0 :(得分:0)

然后你的行

sqlbulk.ColumnMappings.Add("LnNumber", "CUSTOMER_LOAN_NO");

应该说

sqlbulk.ColumnMappings.Add( "LOAN_NO","CUSTOMER_LOAN_NO");

以下是此实用程序的文档:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopycolumnmapping.aspx