大家好,我想讲TXT文件先FILL到DATASET中,获取TXT文件的数量,然后再用SqlBulkCopy将数据写入SQL数据库,现在将TXT数据FILL到DATASET时,FILL那句报错”找不到可安装的 ISAM。“,麻烦各位帮忙指点一下,多谢大家。
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "请选择要导入的文件:";
dlg.Filter = "文本文件(*.txt)|*.txt|所有文件|*.*";
dlg.Multiselect = true; if (dlg.ShowDialog() == DialogResult.OK)
{
string[] FileNames = dlg.FileNames; foreach (string FileName in FileNames)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FileName + ";Extended Properties=Text"; OleDbConnection conn = new OleDbConnection(strConn);
DataSet DS = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter("select * from " + FileName, conn); OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter); adapter.Fill(DS,"x"); MessageBox.Show(DS.Tables[0].Rows.Count.ToString()); }
}
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "请选择要导入的文件:";
dlg.Filter = "文本文件(*.txt)|*.txt|所有文件|*.*";
dlg.Multiselect = true; if (dlg.ShowDialog() == DialogResult.OK)
{
string[] FileNames = dlg.FileNames; foreach (string FileName in FileNames)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FileName + ";Extended Properties=Text"; OleDbConnection conn = new OleDbConnection(strConn);
DataSet DS = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter("select * from " + FileName, conn); OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter); adapter.Fill(DS,"x"); MessageBox.Show(DS.Tables[0].Rows.Count.ToString()); }
}
解决方案 »
- .net的面试题,怎么会出现JVM?
- 100分求2个字符串的处理在正确后结帖马上结贴!!!
- C#中用正则表达式实现,提取内容中url中的字符串中的mtime字符串???
- 如何修改axwebBrower网页内容?
- 母牛生小牛的算法,欢迎大家参与
- 这个异常是怎么回事情,我的代码是msdn上的啊??
- Microsoft Visual Studio 2008窗体开不开
- 请问!在ASP。NET中的网页都是在 Visual Studio 。NET 里完成吗?
- 想听听C#(或者说是.net)和JAVA的区别
- winform调用外部exe,win7下正常 win10错误
- 文件下载,求帮忙!!!!
- 求教有关Excel导入和DataTable的几个问题。
不可以
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FileName + ";Extended Properties=Text"; OleDbConnection conn = new OleDbConnection(strConn);
DataSet DS = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter("select * from " + FileName, conn);先考虑怎么将txt读成string
然后才分割成和数据库对应的
最后插入
然后既然你要插入数据库的东西,那么这个TXT文件肯定必须是有特定的规律来分割的,
读取出来根据这个分隔分别写入数据库。 public string ReadText()
{
string str = string.Empty;
try
{
if (!File.Exists(HttpContext.Current.Server.MapPath("XXX/XXXXX.txt")))
{
return "";
}
StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath("XXX/XXX.txt"), System.Text.Encoding.Default);
String text = sr.ReadToEnd(); //全部读取
//string lineText= sr.ReadLine();//逐行读取
str=text
sr.Dispose();
sr.Close();
}
catch
{ }
return str;
}读取出来之后进行分隔插入数据库,
另外读取肯定是要在服务端的,所以并不是你把客户端的文件选择到了框框里面就能读取到了,要上传到服务端,然后读取服务端保存的路径下面对应的文件,或者直接把文件流传递给读取的方法。
{
List<string> ListStrData = new List<string>(); OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "请选择要导入的文件:";
dlg.Filter = "文本文件(*.txt)|*.txt|所有文件|*.*";
dlg.Multiselect = true; if (dlg.ShowDialog() == DialogResult.OK)
{
string[] FileNames = dlg.FileNames; foreach (string FileName in FileNames)
{
StreamReader Reader = new StreamReader(new FileStream(FileName, FileMode.Open, FileAccess.Read), System.Text.Encoding.Default);
String strConn = "Server=GFKSAS3;UID=sa;PWD=f0reC0urt;Database=Northwind";
SqlConnection conn = new SqlConnection(strConn);
conn.Open(); string insertCmd="insert into test values(@OBJECT,@City,@Type)";
char delimChar = ',';
string[] split = null;
string strTemp = Reader.ReadLine(); while (strTemp != null)
{
ListStrData.Add(strTemp); split = strTemp.Split(delimChar); SqlCommand cmd = new SqlCommand(insertCmd, conn); cmd.Parameters.Add("@OBJECT", SqlDbType.Int).Value = split[0];
cmd.Parameters.Add("@City", SqlDbType.NVarChar).Value = split[1];
cmd.Parameters.Add("@Type", SqlDbType.Int).Value = split[2]; try
{
cmd.ExecuteNonQuery();
} catch (System.Data.SqlClient.SqlException ex)
{
MessageBox.Show(ex.Message);
}
strTemp = Reader.ReadLine();
}
MessageBox.Show(ListStrData.Count.ToString()); Reader.Close();
conn.Close();
}
}
}
然后再把每条数据分割,然后逐条追加到数据库中。