如何一次执行多个请求

时间:2015-02-11 15:39:04

标签: c# sql-server loops request

对于我的实习生,我必须做一些SQL请求。

我尝试将数据从csv文件传输到sql-server数据库

string str = Path.GetFullPath(".");
    String conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=""Text;HDR=No;FMT=Delimited"";Data Source="+str+"";
    string strCnx = "Database=" + ConfigurationManager.AppSettings["base"] + ";data source=" + ConfigurationManager.AppSettings["servername"] + ";User Id=" + ConfigurationManager.AppSettings["user"] + ";Password=" + ConfigurationManager.AppSettings["password"] + ";Connect Timeout=10";
    // Open a sourceConnection to the AdventureWorks database. 
    //using (OleDbConnection sourceConnection = new OleDbConnection(strCnx))
    {
        //sourceConnection.Open();

        // Get data from the source table as a SqlDataReader.
        OleDbConnection cn = new OleDbConnection(conn);
        OleDbCommand commandSourceData = new OleDbCommand("SELECT * FROM [" + ConfigurationManager.AppSettings["csvname"] + "]", cn);
        OleDbDataAdapter da = new OleDbDataAdapter(commandSourceData);
        cn.Open();
        OleDbDataReader reader = commandSourceData.ExecuteReader();

        // Open the destination connection. In the real world you would  
        // not use SqlBulkCopy to move data from one table to the other  
        // in the same database. This is for demonstration purposes only. 
        using (SqlConnection destinationConnection = new SqlConnection(strCnx))
        {
            destinationConnection.Open();

            // Set up the bulk copy object.  
            // Note that the column positions in the source 
            // data reader match the column positions in  
            // the destination table so there is no need to 
            // map columns. 
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))
            {
                bulkCopy.DestinationTableName = "GudsisUser";

                bulkCopy.WriteToServer(reader);
            }
            Console.WriteLine("Press Enter to finish.");
            Console.ReadLine();
        }

但应用程序返回错误消息:无法将字符串值解析为nchar值

我不知道我的程序是否正确

1 个答案:

答案 0 :(得分:1)

我猜你正在寻找的是BULK INSERT(在一个DB请求中做所有事情?)

如果是,请查看答案here