|
[code=csharp]/private void button1_Click(object sender, EventArgs e)
// {
subImportCsv_Dataset("D:\\My Documents\\Visual Studio 2010\\Projects\\ES-TR-Client\\ES-TR-Client\\bin\\Debug","1.csv");
csvToSql("D:\\My Documents\\Visual Studio 2010\\Projects\\ES-TR-Client\\ES-TR-Client\\bin\\Debug","1.csv");
}
public void subImportCsv_Dataset(string filePath, string fileName)
{
string strConn = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=";
strConn += filePath;//这个地方只需要目录就可以了
strConn += ";Extensions=asc,csv,tab,txt;";
OdbcConnection objConn = new OdbcConnection(strConn);
string strSQL = "select * from " + fileName;//文件名,不要带目录
OdbcDataAdapter da = new OdbcDataAdapter(strSQL, objConn);
DataSet ds = new DataSet();
da.Fill(ds);
}
private static bool csvToSql(string filePath, string fileName)
{
string StrConn = "Data Source=.;AttachDbFilename=|DataDirectory|\\App_Data\\Database1.mdf;Integrated Security=True;User Instance=FALSE";
SqlConnection connection = new SqlConnection(StrConn);
try
{
connection.Open();
string cmdtxt = @"insert Table from '" + filePath + fileName + "' with(FIRE_TRIGGERS,FIELDTERMINATOR=',')";
SqlCommand cmd = new SqlCommand(cmdtxt, connection);
cmd.ExecuteNonQuery();
return true;
}
catch (System.Exception ex)
{
// log.WriteLine("文件:{0} error:{1}", fileName, ex.Message);
// log.Flush();
return false;
}
}[/code]这一段代码是从网上找的,好想操作的是sqlserver,如有大神望不吝赐教QQ 1311937460,在线等
|
|