資料匯出和匯入

2021-06-18 12:24:09 字數 2911 閱讀 9080

以winfrom為例(控制台也可以)

1、由記事本匯入資料庫中

(1)使用流檔案與記事本建立聯絡

filestream filestream = file.open(@"h:\studentinfo.txt", filemode.openorcreate, fileaccess.read);

streamreader reader=new streamreader(filestream,encoding.default);防止出現亂碼

(2)開啟資料庫

string sql = configurationmanager.connectionstrings["sqlseverstr"].connectionstring;//新增引用

sqlconnection conn = new sqlconnection(sql);

conn.open();

sqlcommand command = conn.createcommand();

command.commandtext = "insertintot_student(name,id,***,mobile,address,xibie,jiangpin,score) 

values(@name,@id,@***,@mobile,@address,@xibie,@jiangpin,@score)";

(3)將記事本中的資料匯入資料庫中

string result = "";

while((result=reader.readline())!=null)

(4)關閉流關閉資料庫的連線,釋放記憶體

conn.close();

conn.dispose();

command.dispose();

filestream.close();

filestream.dispose();

reader.close();

reader.dispose();

messagebox.show("匯入成功");//檢查是否匯入成功

2、資料匯出(資料庫匯出到記事本中)

(1)與資料庫連線

string sql = configurationmanager.connectionstrings["sqlseverstr"].connectionstring;

sqlconnection conn = new sqlconnection(sql);

sqlcommand command = conn.createcommand();

command.commandtext = "select * from t_student";

datatable dt = new datatable();

sqldataadapter adapter = new sqldataadapter(command);

adapter.fill(dt);

(2)與記事本建立連線

filestream filestream = file.open(@"h:\studentinfo.txt",filemode.openorcreate,fileaccess.write);

streamwriter write = new streamwriter(filestream,encoding.default);

(3)將資料匯入記事本中

string name = "";

string id = "";

string *** = "";

string mobile = "";

string address = "";

string xibie = "";

string jiangpin = "";

string score = "";

for (int i = 0; i < dt.rows.count; i++)

(4)關閉連線釋放記憶體

write.close();

write.dispose();

filestream.close();

filestream.dispose();

messagebox.show("匯出成功");//測試是否匯出成功

其中使用的配置檔案在解決方案管理器中右擊專案新增新建項有應用配置檔案(檔名最好不要改)新增

資料匯入和匯出

1.資料匯入 將資料從檔案中匯入到資料庫。1.從檔案中讀取資料 2.將資料插入資料庫 注意 如果讀取檔案的編碼與檔案儲存的編碼不一致,容易出現亂碼 沒有第二個引數額過載是採用utf8編碼 ienumerable lines file.readlines filepath,encoding.defau...

Hive資料匯入和匯出

1.將select的結果放到乙個的的 中 首先要用create table建立新的 insert overwrite table test select uid,name from test2 2.將select的結果放到本地檔案系統中 insert overwrite local director...

Django 資料匯入和匯出

本文主要講資料庫的遷移方法,包含不同資料庫,如 sqlite3,mysql,postgresql 之間資料遷移方案,以及資料在不同機器上遷移方案 1 1 python manage.py dumpdata blog blog dump.json 1 python manage.py loaddata...