在Stackoverflow上爲每個人問候! 我一直在研究這個問題,並沒有運氣。我想將一個csv文件導入到我的mysql數據庫中,但是我一直收到錯誤1:「數據太長,無法在列1上添加列」,錯誤2:「參數'@sku'已被定義」。錯誤1出現一次,錯誤2,我想重複,直到循環結束。我的代碼如下:使用C在MySql中導入csv#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using MySql.Data;
using MySql.Data.MySqlClient;
//using Microsoft.VisualBasic.FileIO;
namespace cvsimporttrial
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.DefaultExt = "*.csv";
fd.Filter = "CSV files (*.csv)|*.csv";
fd.ShowDialog();
textBox1.Text = fd.FileName;
}
private void btnSaveToDatabase_Click(object sender, EventArgs e)
{
string constr = "server=localhost;user id=*****;password=*****;persistsecurityinfo=True;database=*****";
MySqlConnection con = new MySqlConnection(constr);
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into testimport values (@sku,@itemname,@itemdescription,@upc)";
DataTable dt = new DataTable();
try
{
con.Open();
cmd.Prepare();
// Open the text file using a stream reader.
using (StreamReader sr = new StreamReader(textBox1.Text))
{
dt.Columns.Add("sku");
dt.Columns.Add("ItemName");
dt.Columns.Add("ItemDescription");
dt.Columns.Add("upc");
while (!sr.EndOfStream)
{// Read the stream to a string, and write the string to the console.
//DataRow dr = dt.NewRow();
String line = sr.ReadLine(); //sr.ReadToEnd();
string[] columns = line.Split(',');
foreach (DataColumn cols in dt.Columns)
{ DataRow dr = dt.NewRow();
dr["sku"]= columns[0];
if (columns[0] == "sku") { continue; }//skip the heading as database already has heading, is there any other way to achieve this.
dr["ItemName"]= columns[1];
dr["ItemDescription"] = columns[2];
dr["upc"]= columns[3];
dt.Rows.Add(dr);
//Console.WriteLine(columns[3]);
}//foreach loop
//Console.WriteLine(line);
//Console.WriteLine(columns[0]);
//Console.WriteLine(columns[1]);
//Console.WriteLine(columns[2]);
//Console.WriteLine(columns[3]);
try
{
foreach (DataRow r in dt.Rows)
{
//cmd.Parameters["@sku"].Value = r["sku"];
cmd.Parameters.AddWithValue("@sku", r["sku"]);
cmd.Parameters.AddWithValue("@itemname", r["ItemName"]);
cmd.Parameters.AddWithValue("@itemdescription", r["ItemDescription"]);
cmd.Parameters.AddWithValue("@upc", r["upc"]);
cmd.ExecuteNonQuery();
}
}//mysql try block
catch (MySqlException mse) { MessageBox.Show(mse.Message); }//mysql catch
}//while loop
}
}//try block for file stream
catch (Exception ex) { Console.WriteLine(ex.Message); }//catch for file stream
}//btnSaveToDatabase_click event
}
}
我的csv文件樣本: SKU,ITEMNAME,ItemDescription,UPC XY-1-12-ZX,零度可樂易拉罐百毫升 - 12包,「讓口感清爽可樂與零糖「,6111111113
謝謝你的時間。
這是嘗試重新創建LOAD DATA IN FILE嗎? – e4c5
我嘗試了「LOAD DATA INFILE test.csv INTO TABLE testimport FIELDS TERMINATED BY','LINES TERMINATED BY'\ n'IGNORE 1 LINES(sku,ItemName,ItemDescription,upc)」但是卻死光了。我是否正確編寫了這條sql語句來查詢看起來像我上面描述的csv文件? – dshock9
什麼是所謂的死衚衕。是否有錯誤? – e4c5