excel匯入資料庫

2022-02-11 11:43:46 字數 3307 閱讀 3819

日常工作中,感覺一些基礎知識需要做下筆記,可能是剛畢業的緣故吧,還保持著做筆記的習慣,但根據以往經驗,紙質筆記最多保持一年,過後想找已是難過登天。電子版筆記感覺很不錯,尤其是發布到網路中。筆記內容是本人遇到的感覺可能會有些用的東西,很是瑣碎,記錄在中,僅供學習參考。

1、將資料庫中的內容顯示到乙個窗體介面中,此次將它顯示到

datagrid中。

思路:可以說沒有什麼思路,很簡單。讀取資料庫中某個**的內容,將其放到

dataset

中,然後將其作為

datagrid

的源即可。

貼**:

string sqlstr = "

server=192.168.100.46;database=publicprogram;user id=sa;pwd=typt";

using (sqlconnection sqlconn = new

sqlconnection(sqlstr))

}

2、獲取excel**中的內容,此次將excel中的內容儲存資料庫中

思路:獲取excel的路徑,通過oledb方式獲取excel檔案的資料,儲存到dataset中。接著可以單個獲取dataset中值或者匯入到資料庫中。

獲取excel的路徑:

openfiledialog openfiledialog = new

openfiledialog();

dialogresult result =openfiledialog.showdialog();

if (result ==system.windows .forms .dialogresult .cancel )

string filename =openfiledialog.filename;

tbpath.text = filename;

通過oledb方式獲取excel檔案,並儲存到dataset

string strconn = "

provider=microsoft.ace.oledb.12.0;

" + "

data source=

" + filepath + "

;" + "

extended properties=\"excel 12.0;hdr=yes;imex=1\"";

oledbconnection oleconn = new

oledbconnection(strconn);

oleconn.open();

string sql = string.format("

select * from [$]

", sheetname);

oledbdataadapter oledbdadap = new

oledbdataadapter(sql, oleconn);

dataset ds = new

dataset();

oledbdadap.fill(ds, sheetname);

oleconn.close();

獲取dataset後,可以將它訪問到資料庫中,也可以從其中得到需要的資料資訊

存到資料庫中:

using (sqlbulkcopy bcp = new

sqlbulkcopy(connectionstring))

如果獲取單個資料,舉例簡單說明一下:

int length = ds.tables[0

].rows.count;

int len = ds.tables[0

].columns.count;

id = convert.toint32(ds.tables[0].rows[length-1][len-2

]); pwd = convert.toint32(ds.tables[0].rows[1][1

]);

for (int i = 0; i < length - 1; i++)

3、wpf設定datagrid的一些屬性:

在第一道題中寫到了將dataset作為datagrid的資料來源,這裡僅介紹在wpf中datagrid的一些用法。

改變datagrid

的標題行的一些屬性:

<

datagrid.columnheaderstyle

>

<

style

targettype

="datagridcolumnheader"

>

<

setter

property

="background"

value

="black"

/>

<

setter

property

="foreground"

value

="white"

/>

<

setter

property

="fontsize"

value

="20"

/>

<

setter

property

="horizontalcontentalignment"

value

="center"

/>

<

setter

property

="verticalcontentalignment"

value

="center"

/>

style

>

datagrid.columnheaderstyle

>

將標題繫結到一些資料來源中,設定其中的內容居中

首先需要將控制項設定樣式:

然後呼叫:

筆停此處。

繼續學習,繼續前行。

///

///查詢excel電子**新增到dataset

/// ///

///public dataset execleds(string

filenameurl)

$]", "

sheet1");

oledbdataadapter odda = new

oledbdataadapter(strsql, conn);

odda.fill(ds,

"hou");

return

ds; }

excel匯入資料庫

在你的 中增加一列,利用excel的公式自動生成sql語句 concatenate 函式 具體方法如下 1 增加一列 假設是d列 2 在第一行的d列,就是d1中輸入公式 concatenate insert into table col1,col2,col3 values a1,b1,c1,3 此時...

Excel匯入資料庫

一 在excel中 新建一列,如把列名定為ab,下面放你的資料,比如1,2,3 二 開啟sql企業管理器,右擊你所需要匯入的資料庫,選擇匯入資料,下一步,資料來源選擇microsoft eccel 97 2000,選擇excel檔案繼續下一步,往下按,選擇sheet1 或者2,3 看你把資料放在那一...

EXCEL資料匯入資料庫

1 類設計,excel要據配置讀入dataset using system using system.data using system.collections using system.data.oledb namespace hkh.common 從excel匯入資料到dataset,帶有虛函式...