本文介绍了将csv文件导入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将exel文件导入数据库但出错了参数化查询'(@ OrderID int,@ CustomerName varchar(20),@ Address varchar(50),@ Em'需要参数'@OrderID',这不是提供。



i am importing exel file into database but giving error The parameterized query '(@OrderID int,@CustomerName varchar(20),@Address varchar(50),@Em' expects the parameter '@OrderID', which was not supplied.

DataTable dt = new DataTable();
        dt.Columns.Add("OrderID");
        dt.Columns.Add("CustomerName");
        dt.Columns.Add("Address");
        dt.Columns.Add("Email");
        dt.Columns.Add("Total");
        string path = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Imported file/" + path));
        path = Server.MapPath("~/Imported file/" + path);
        TextFieldParser txtp = new TextFieldParser(path);
        txtp.Delimiters = new string[] { "," };
        txtp.TrimWhiteSpace = true;

        while (!txtp.EndOfData)
        {
            string[] fields = txtp.ReadFields();
            dt.Rows.Add(fields.Equals("OrderID"));
            dt.Rows.Add(fields.Equals("CustomerName"));
            dt.Rows.Add(fields.Equals("Address"));
            dt.Rows.Add(fields.Equals("Email"));
            dt.Rows.Add(fields.Equals("Total"));

        }

        con = Connectivity.GetConnection();
        string import = "Insert into Customer (OrderID,CustomerName,Address,Email,Total) values(@OrderID,@CustomerName,@Address,@Email,@Total)";
        SqlCommand cmd = new SqlCommand(import, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);

        //cmd.Parameters.AddWithValue("@OrderID", "OrderID");
        //cmd.Parameters.AddWithValue("@CustomerName","CustomerName");
        //cmd.Parameters.AddWithValue("@Address",  "Address");
        //cmd.Parameters.AddWithValue("@Email",  "Email");
        //cmd.Parameters.AddWithValue("@Total", "Total");

        //SqlCommand cmd = new SqlCommand();
        //cmd.CommandType = CommandType.Text;
        //cmd.CommandText = import;
        //cmd.Connection = con;

        cmd.Parameters.Clear();
        cmd.Parameters.Add("@OrderID", SqlDbType.Int);
        cmd.Parameters.Add("@CustomerName", SqlDbType.VarChar, 20, "CustomerName");
        cmd.Parameters.Add("@Address", SqlDbType.VarChar, 50, "Address");
        cmd.Parameters.Add("@Email", SqlDbType.VarChar, 50, "Email");
        cmd.Parameters.Add("@Total", SqlDbType.Float);
        // SqlDataAdapter da = new SqlDataAdapter();
        cmd.ExecuteNonQuery();
        // da.InsertCommand = cmd;
        int result = da.Update(dt);

推荐答案



while (!txtp.EndOfData)
        {
            string[] fields = txtp.ReadFields();
            var newRow = dt.NewRow();
            newRow.ItemArray = fields;
            dt.Rows.Add(newRow);

        }


这篇关于将csv文件导入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:37