将DBF文件导入Sqlserver数据库
項(xiàng)目中的問題:用戶選擇N個(gè)dbf文件導(dǎo)入sql2005數(shù)據(jù)庫(kù),由于每年dbf表結(jié)構(gòu)都在變化,所以在sql2005中根本就不存在,需要每年根據(jù)dbf的結(jié)構(gòu)自動(dòng)建表。(文章來自http://blog.csdn.net/whuyi/article/details/5990205)
解決方法(摘抄自網(wǎng)絡(luò)):
--方法一:
select * into 要生成的SQL表名 from OPENROWSET('MICROSOFT.JET.OLEDB.4.0','dBase IV;HDR=NO;IMEX=2;DATABASE=c:/','select * from dbf表名.dbf')
--方法二:
select * into 要生成的SQL表名 from OPENROWSET('MICROSOFT.JET.OLEDB.4.0','dBase III;HDR=NO;IMEX=2;DATABASE=c:/','select * from dbf表名.dbf')
--方法三:
select * into 要生成的SQL表名 from openrowset('MSDASQL','Driver=Microsoft Visual FoxPro Driver;SourceType=DBF;SourceDB=c:/','select * from dbf表名.DBF')
--用前兩種方法導(dǎo)入SQL SERVER后,源表再用VFP打開就不提示“不能存取文件”,說明語(yǔ)句執(zhí)行后就把源表關(guān)閉了。不過也有不盡人意的地方,就是用前兩種方法導(dǎo)入后,源表中的字符型字段導(dǎo)入后SQL表字段對(duì)應(yīng)變成NVARCHAR了。
--第三種方法有一個(gè)缺點(diǎn):把DBF表導(dǎo)入SQL Server中后,馬上用VISUAL FOXPRO打開DBF表,會(huì)提示“不能存取文件”,即這個(gè)表還被SQL打開著呢。可是過了1分鐘左右,再打開DBF表就可以了,說明經(jīng)過一段時(shí)間后查詢分析器才把這個(gè)表關(guān)閉。
可以直接將dbf文件導(dǎo)入sqlserver數(shù)據(jù)庫(kù),也可以先將dbf文件導(dǎo)入dataset,再將dataset的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)。dbf文件導(dǎo)入dataset的具體實(shí)現(xiàn)方法如下:
需要引入system.data.Odbc包
public DataSet importDbfToDataSet(string FilePath, string tabname)
{
string strConnection = @"Dsn=Visual FoxPro Tables;sourcedb=" + FilePath.Substring(0, FilePath.LastIndexOf("http://")) + ";sourcetype=DBF;exclusive=No;backgroundfetch=Yes;collate=Machine";
//對(duì)于連接串,注意版本問題
string strSelect = "SELECT * FROM " + tabname;
OdbcConnection thisConnection = new OdbcConnection(strConnection);
thisConnection.Open();
OdbcDataAdapter thisAdapter = new OdbcDataAdapter(strSelect, thisConnection);
DataSet thisDataSet = new DataSet();
try
{
thisAdapter.Fill(thisDataSet);
}
catch (Exception e)
{
throw e;
}
finally
{
thisConnection.Close();
}
return thisDataSet;
}
View Code
下面的程序通過Timer定時(shí)器,設(shè)置定時(shí)將本地dbf文件直接存入數(shù)據(jù)庫(kù)表,該dbf文件會(huì)定時(shí)被覆蓋掉。
class Connect
{
//定義連接字符串,連接對(duì)象,命令對(duì)象
private String connectionstr;
private SqlConnection connection;
private SqlCommand command;
private DataSet dataset;
public Connect()
{
connectionstr = "Server=192.168.88.59;Initial Catalog=Test; User ID=sa;Password=sasa;";
connection = new SqlConnection(connectionstr);
dataset = new DataSet();
command = connection.CreateCommand();
connection.Open();
}
public void CreateTimer()
{
Timer timer = new Timer();
timer.Enabled = true;
timer.Interval = 60 * 1000;//設(shè)置一分鐘
timer.Elapsed+=new ElapsedEventHandler(timer_Elapsed);
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
Connect c = new Connect();
int minute = e.SignalTime.Minute;
int iminute = 5;
if(minute==iminute)//設(shè)置每個(gè)小時(shí)的第五分鐘執(zhí)行
c.Insert();
}
private void Insert()
{
//string filepath = "C;//";
//將dbf文件導(dǎo)入指定數(shù)據(jù)庫(kù)的表
string creattb = "select * into tablename from OPENROWSET('MSDASQL','driver=Microsoft visual foxpro driver; sourcedb=c://;SourceType=DBF','select * from RSZ1031.dbf')";
SqlCommand mycommand = new SqlCommand(creattb, connection);
mycommand.ExecuteNonQuery();
}
}
1.無法初始化鏈接服務(wù)器 "(null)" 的 OLE DB 訪問接口 "msdasql" 的數(shù)據(jù)源對(duì)象”;
原因是安裝sql2005數(shù)據(jù)庫(kù)的系統(tǒng)中沒有 VFPODBC驅(qū)動(dòng),到http://msdn.microsoft.com/en-us/vfoxpro/bb190233.aspx下載并安裝
2.SQL Server 阻止了對(duì)組件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的訪問;
原因是因?yàn)椤肮δ艿耐鈬鷳?yīng)用配置器”中沒有“啟用openrowset和opendatasource支持”,只需要打開“功能的外圍應(yīng)用配置器”設(shè)置一下就可以。
總結(jié)
以上是生活随笔為你收集整理的将DBF文件导入Sqlserver数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FJUT3703 这还是一道数论题(二分
- 下一篇: 3dmax怎么制作个性卧室效果图