将csv文件读取到datable中并插入到mysql数据库中 [英] reading csv file into datable and insert to mysql database

查看:104
本文介绍了将csv文件读取到datable中并插入到mysql数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

检查此代码,

Hi Guys,

Check this code,

string file = FileUpload1.PostedFile.FileName;
        file = Path.GetFileName(file);
        string filepath = Server.MapPath("~/csv/")+file;
        FileUpload1.SaveAs(filepath); 
        StreamReader sr = new StreamReader(filepath);
        string line = sr.ReadLine();
        string[] value = line.Split(';');
        DataTable dt = new DataTable();
        DataRow row;
        foreach (string dc in value)
        {
            dt.Columns.Add(new DataColumn(dc));
        }
        while (!sr.EndOfStream )
        {
            value = sr.ReadLine().Split(';');
            if(value.Length == dt.Columns.Count)
            {
                row = dt.NewRow();
                row.ItemArray = value;
                dt.Rows.Add(row);
            }
        }

        string sql = "Insert into company(CompanyName,ContactPerson,Email,Tel,Address,DemurrageOffrered)values(@CompanyName,@ContactPerson,@Email,@Tel,@Address,@DemurrageOffered)";
        sc.Open();
        foreach (DataRow r in dt.Rows)
        {
            MySqlCommand cmd = sc.CreateCommand();
            cmd.CommandText = sql;
            cmd.Parameters.AddWithValue("@CompanyName", r["CompanyName"]);
            cmd.Parameters.AddWithValue("@ContactPerson", r["ContactPerson"]);
            cmd.Parameters.AddWithValue("@Email", r["Email"]);
            cmd.Parameters.AddWithValue("@Tel", r["Tel"]);
            cmd.Parameters.AddWithValue("@Address", r["Address"]);
            cmd.Parameters.AddWithValue("@DemurrageOffered", r["DemurrageOffered"]);

        }
        sc.Close();
        Response.Write("ook");


它正在以"NO ERRORS"执行,但数据未插入mysql数据库.
它正在将csv文件读入数据表,但未插入mysqldatabase.

请帮忙!!! :)

谢谢和问候
Harsha


It is executing with "NO ERRORS" but the data is not inserted into mysql database.
It is reading the the csv file into datatable but not inserting to mysqldatabase.

Please help!!! :)

Thanks and regards
Harsha

推荐答案

您从未执行过该命令.请参阅:
http://msdn.microsoft.com/en-us/library/system. data.sqlclient.sqlcommand.aspx [ ^ ].

请参见方法"部分.您需要使用包含子字符串"Execute"的名称来调用方法之一.由于您的命令是插入",因此可以在特定情况下使用System.Data.SqlClient.SqlCommand.ExecuteNonQuery,请参阅:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx [ ^ ].

—SA
You never executed the command. Please see:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx[^].

Please see the section "Methods". You need to call one of the methods with the name containing the sub-string "Execute". As your command is "Insert", you can use System.Data.SqlClient.SqlCommand.ExecuteNonQuery in your particular case, pleas see:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx[^].

—SA


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

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