求高手解释一段C#的代码,操作数据库的,最好能按句注释下
privatevoidbutton1_Click(objectsender,EventArgse){subImportCsv_Dataset("C:\\data\\","...
private void button1_Click(object sender, EventArgs e)
{
subImportCsv_Dataset("C:\\data\\","AHU1_数据记录0.csv");
csvToSql("C:\\data\\","AHU1_数据记录0.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=True";
SqlConnection connection = new SqlConnection(StrConn);
try
{
connection.Open();
string cmdtxt = @"bulk 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;
}
} 展开
{
subImportCsv_Dataset("C:\\data\\","AHU1_数据记录0.csv");
csvToSql("C:\\data\\","AHU1_数据记录0.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=True";
SqlConnection connection = new SqlConnection(StrConn);
try
{
connection.Open();
string cmdtxt = @"bulk 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;
}
} 展开
2个回答
展开全部
//按钮触发事件
private void button1_Click(object sender, EventArgs e)
{
//调用subImportCsv_Dataset方法,传递文件的路径和文件名
subImportCsv_Dataset("C:\\data\\","AHU1_数据记录0.csv");
//调用csvToSql方法,传递文件的路径和文件名
csvToSql("C:\\data\\","AHU1_数据记录0.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;//文件名,不要带目录
//这边相当于把一个csv文件当做一张表格连接到后取得里面的所有数据
OdbcDataAdapter da = new OdbcDataAdapter(strSQL, objConn);
DataSet ds = new DataSet();
//将得到的数据填到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=True";
//定义连接对象
SqlConnection connection = new SqlConnection(StrConn);
try
{
//打开数据库连接对象
connection.Open();
//SQL语句字符串 大致的意思是将文件中的数据插入到名为Table的表中,用‘,’作为分隔符
string cmdtxt = @"bulk insert Table from '" + filePath + fileName + "' with(FIRE_TRIGGERS,FIELDTERMINATOR=',')";
//定义SqlCommand对象
SqlCommand cmd = new SqlCommand(cmdtxt, connection);
//执行语句
cmd.ExecuteNonQuery();
//执行成功返回true
return true;
}
catch (System.Exception ex)
{
//执行错误的话返回false
// log.WriteLine("文件:{0} error:{1}", fileName, ex.Message);
// log.Flush();
return false;
}
}
大致的意思是点下按钮将数据文件转到DataSet中并且保存到数据库中的Table表里面返回是否成功
更多追问追答
追问
亲 我想插入我建的一个表应该怎么改这段代码???
dsdate.CommandText = "SELECT COUNT(*) FROM sqlite_master where type='table' and name='DateSource'";
我创建的名为DateSource表
追答
不明白你什么意思,是建了个DateSource的表然后想把数据文件中的数据导到这张表里面么?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询