寫這篇文章時,我也拿不好主意到底是該post上來呢,還是不post!因為這實在好像沒有什么的。后來轉念一想,反正是blog嗎,不就是寫寫自己的心情之類的嘛,管他有沒有技術含量,只要不放到首頁上影響整體博客的技術形象就可以了。所以我就把這篇文章post上來了。高手就免看了。:)
事情源于一個網友的一個要求:她自己說是一個作業,要將pubs中的所有用戶表的內容分別用xml文件導出保存起來。本來想當時給她答案的,可真不好彩的是被一個客戶的電話逮住,只好回到家中為她做一下。我也不清楚,她是扮豬吃老虎還是真是初學者。不過從和她對話來看,倒也是性情中女子。應該還是蠻可愛的一個女孩子啦!隱隱約約談話當中,我想她可能自己也已經完成這個“作業”的,可能她自己拿不準自己能不能“及格”才問我的。呵呵...
拿著這份要求,很快,腦子里就閃現dataset有一個writexml的方法。事實上,我也是采用這個簡單的方法來完成她的作業的。當然,在這個小小的作業上,我還是將幾乎將ado.net用了個遍,包括sqlconnection,sqldataadapter,sqlcommand,sqldatareader和dataset。另外還使用了一個arraylist數組。arraylist還真是好東西,vs6.0好象是沒有的吧。(不知道有沒有,反正我以前好象沒有用過,呵呵...)整個代碼實現,我都只在一個button的click事件中實現。代碼不多。干脆一同post上來。
private void button1_click(object sender, system.eventargs e)
{
string strconn = "data source=localhost;initial catalog=pubs;" + "integrated security=sspi";
string strsql = "select name from sysobjects where xtype='u' and status>=0";
sqlconnection sqlconn = new sqlconnection(strconn);
sqlcommand sqlcomm = new sqlcommand(strsql,sqlconn);
sqldatareader sqlread = null;
dataset ds = null;
arraylist al = new arraylist();
if(sqlconn .state == connectionstate.closed) sqlconn.open();
try
{
sqlread = sqlcomm.executereader();
while(sqlread.read())
{
string strtablename = sqlread.getvalue(0).tostring();
al.add(strtablename);
}
sqlread.close();
for(int i =0;i<al.count;i++)
{
string strtable = "select * from "+al[i].tostring();
sqldataadapter sqladapter = new sqldataadapter(sqlcomm);
ds = new dataset();
sqlread.close();
sqladapter.fill(ds,al[i].tostring());
ds.writexml(al[i].tostring()+".xml");
}
messagebox.show("finish it!");
}
catch(exception e)
{
messagebox.show(e.tostring());
}
finally
{
if(sqlconn.state == connectionstate.closed) sqlconn.close();
}
}
沒有最好,只求更好。如果那位高手不幸看到了這篇post,并且認為還有更好的解決方案,歡迎回復討論!
新聞熱點
疑難解答