C#如何批量导入txt文件内容到sqlserver中的某一列?
时间:2011-12-15
来源:互联网
代码如下:
DirectoryInfo theDir = new DirectoryInfo(tbFilesPath.Text.Trim());
FileInfo[] fileInfo = theDir.GetFiles(); //目录下的文件
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
int errcount = 0;
foreach (FileInfo fInfo in fileInfo)
{
string id = fInfo.Name.Replace(".txt", "");
StreamReader fr = new StreamReader(@tbFilesPath.Text.Trim() + @"\" + fInfo.Name, System.Text.Encoding.GetEncoding("gb2312"));
string text = fr.ReadToEnd().Replace("'", "");
fr.Close();
fr.Dispose();
string sql = "INSERT into bvcpllst " +
"VALUES('dbs','" + id + "',null,'" + text + "')";
cmd.CommandText = sql;
if (cmd.ExecuteNonQuery() < 1)
{
MessageBox.Show("id=[" + id + "]的记录插入失败,请检查!");
errcount += 1;
break;
}
}
文件夹中有大约15000个txt文件,程序运行时出错如下:
SQL Server 内部错误。文本管理器无法继续执行当前语句。
DirectoryInfo theDir = new DirectoryInfo(tbFilesPath.Text.Trim());
FileInfo[] fileInfo = theDir.GetFiles(); //目录下的文件
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
int errcount = 0;
foreach (FileInfo fInfo in fileInfo)
{
string id = fInfo.Name.Replace(".txt", "");
StreamReader fr = new StreamReader(@tbFilesPath.Text.Trim() + @"\" + fInfo.Name, System.Text.Encoding.GetEncoding("gb2312"));
string text = fr.ReadToEnd().Replace("'", "");
fr.Close();
fr.Dispose();
string sql = "INSERT into bvcpllst " +
"VALUES('dbs','" + id + "',null,'" + text + "')";
cmd.CommandText = sql;
if (cmd.ExecuteNonQuery() < 1)
{
MessageBox.Show("id=[" + id + "]的记录插入失败,请检查!");
errcount += 1;
break;
}
}
文件夹中有大约15000个txt文件,程序运行时出错如下:
SQL Server 内部错误。文本管理器无法继续执行当前语句。
作者: HerringYang 发布时间: 2011-12-15
可能是插入的内容太多了
你debug把SQl那出来运行下不就行了?
你debug把SQl那出来运行下不就行了?
作者: jiangfling 发布时间: 2011-12-15
断点跟踪下看获取的text,还有插入语句有错没?
作者: enter89 发布时间: 2011-12-15
我把语句改成了
SqlConnection conn = new SqlConnection(connString);
conn.Open();
try
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select id,dbname,gid,title,fulltext from bvcpllst where 1=0", conn);
da.InsertCommand = new SqlCommand("Insert Into bvcpllst(dbname,gid,title,fulltext) values(@dbname,@gid,@title,@fulltext)", conn);
da.InsertCommand.Parameters.Add("@dbname", SqlDbType.Char, 20, "dbname");
da.InsertCommand.Parameters.Add("@gid", SqlDbType.VarChar, 50, "gid");
da.InsertCommand.Parameters.Add("@title", SqlDbType.VarChar, 2000, "title");
da.InsertCommand.Parameters.Add("@fulltext", SqlDbType.NText, 1073741823, "fulltext");
da.InsertCommand.UpdatedRowSource = System.Data.UpdateRowSource.None;
da.UpdateBatchSize = 0;
da.SelectCommand.CommandTimeout = 0;
da.InsertCommand.CommandTimeout = 0;
DataSet ds = new DataSet();
da.Fill(ds);
GetNameAndTranslate(ds.Tables[0]);
da.Update(ds.Tables[0]);
ds.Tables[0].Clear();
da.Dispose();
ds.Dispose();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
private void GetNameAndTranslate(DataTable dt)
{
DirectoryInfo theDir = new DirectoryInfo(tbFilesPath.Text.Trim());
FileInfo[] fileInfo = theDir.GetFiles(); //目录下的文件
foreach (FileInfo fInfo in fileInfo)
{
string gid = fInfo.Name.Replace(".txt.txt", "");
StreamReader fr = new StreamReader(@tbFilesPath.Text.Trim() + @"\" + fInfo.Name, System.Text.Encoding.GetEncoding("gb2312"));
string fulltext = fr.ReadToEnd().Replace("'", "");
fr.Close();
fr.Dispose();
DataRow dr = dt.NewRow();
dr["dbname"] = "chl";
dr["gid"] = gid;
dr["title"] = null;
dr["fulltext"] = fulltext;
dt.Rows.Add(dr);
}
}
现在程序插入前三个10000条没问题了,后面再插第四个10000条,程序一直运行状态????????
SqlConnection conn = new SqlConnection(connString);
conn.Open();
try
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select id,dbname,gid,title,fulltext from bvcpllst where 1=0", conn);
da.InsertCommand = new SqlCommand("Insert Into bvcpllst(dbname,gid,title,fulltext) values(@dbname,@gid,@title,@fulltext)", conn);
da.InsertCommand.Parameters.Add("@dbname", SqlDbType.Char, 20, "dbname");
da.InsertCommand.Parameters.Add("@gid", SqlDbType.VarChar, 50, "gid");
da.InsertCommand.Parameters.Add("@title", SqlDbType.VarChar, 2000, "title");
da.InsertCommand.Parameters.Add("@fulltext", SqlDbType.NText, 1073741823, "fulltext");
da.InsertCommand.UpdatedRowSource = System.Data.UpdateRowSource.None;
da.UpdateBatchSize = 0;
da.SelectCommand.CommandTimeout = 0;
da.InsertCommand.CommandTimeout = 0;
DataSet ds = new DataSet();
da.Fill(ds);
GetNameAndTranslate(ds.Tables[0]);
da.Update(ds.Tables[0]);
ds.Tables[0].Clear();
da.Dispose();
ds.Dispose();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
private void GetNameAndTranslate(DataTable dt)
{
DirectoryInfo theDir = new DirectoryInfo(tbFilesPath.Text.Trim());
FileInfo[] fileInfo = theDir.GetFiles(); //目录下的文件
foreach (FileInfo fInfo in fileInfo)
{
string gid = fInfo.Name.Replace(".txt.txt", "");
StreamReader fr = new StreamReader(@tbFilesPath.Text.Trim() + @"\" + fInfo.Name, System.Text.Encoding.GetEncoding("gb2312"));
string fulltext = fr.ReadToEnd().Replace("'", "");
fr.Close();
fr.Dispose();
DataRow dr = dt.NewRow();
dr["dbname"] = "chl";
dr["gid"] = gid;
dr["title"] = null;
dr["fulltext"] = fulltext;
dt.Rows.Add(dr);
}
}
现在程序插入前三个10000条没问题了,后面再插第四个10000条,程序一直运行状态????????
作者: HerringYang 发布时间: 2011-12-15
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28