自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

利用C#對XML文檔和數(shù)據(jù)庫操作的四個(gè)技術(shù)節(jié)點(diǎn)

數(shù)據(jù)庫
本文我們主要介紹了C#對XML文檔和數(shù)據(jù)庫操作的四個(gè)技術(shù)節(jié)點(diǎn),包括:圖像上傳到服務(wù)器、動(dòng)態(tài)處理XML文檔、將數(shù)據(jù)添加到數(shù)據(jù)庫以及將讀出來的數(shù)據(jù)存回XML文檔,并給出了詳細(xì)的實(shí)現(xiàn)代碼,希望能夠?qū)δ兴鶐椭?/div>

利用C#對XML文檔數(shù)據(jù)庫的操作對我們初學(xué)者來說是比較難入手的,但是只要我們通過大量的練習(xí),依然可以很快的掌握。本文我們就介紹了利用C#對XML和數(shù)據(jù)庫操作的四個(gè)技術(shù)節(jié)點(diǎn),接下來就讓我們來一起了解一下吧。

首先執(zhí)行:

 

  1. protected void Button2_Click(object sender, EventArgs e)  
  2.  
  3. {  
  4.  
  5. SavaFileXml();   ///通過調(diào)用這個(gè)方法,然后再調(diào)用其他的方法,進(jìn)而實(shí)現(xiàn)所要求實(shí)現(xiàn)的功能。  
  6.  

 

接下來我們就來介紹這些技術(shù)。

技術(shù)節(jié)點(diǎn)一:將圖像上傳到服務(wù)器上面

 

 

  1. private string UpImg() {   
  2.  
  3. ///將圖像上傳到服務(wù)器中去。  
  4.  
  5. ///  
  6.  
  7. if (this.FileUP.HasFile == false)  
  8.  
  9. {///判斷是否為空  
  10.  
  11. Label1.Text = "請選擇要上傳的圖像";  
  12.  
  13. //Response.Write("<script>alert('請選擇要上傳的文件!')</script>");  
  14.  
  15. return null;  
  16.  
  17. }  
  18.  
  19. ///獲取圖像  
  20.  
  21. string FileName = this.FileUP.FileName;  
  22.  
  23. ///獲取圖像的類型.  
  24.  
  25. //string FileType = FileName.Substring(FileName.LastIndexOf('.')+1);  
  26.  
  27. string fType = this.FileUP.PostedFile.ContentType;  
  28.  
  29. if (fType == "image/bmp" || fType == "image/gif" || fType == "image/pjpeg" || fType == "image/x-png")  
  30.  
  31. {  
  32.  
  33. if(File.Exists(Server.MapPath("~/newsimages/" + FileName))==true){///判斷該圖像是否已經(jīng)存在了.  
  34.  
  35. Label1.Text = "該圖像已經(jīng)存在了!";  
  36.  
  37. return null;  
  38.  
  39. }  
  40.  
  41. this.FileUP.PostedFile.SaveAs(Server.MapPath("~/newsimages/" + FileName));  
  42.  
  43. Response.Write("<script>alert('正確了!')</script>");  
  44.  
  45. //return FileName;///返回圖像名稱  
  46.  
  47. }  
  48.  
  49. else  
  50.  
  51. {  
  52.  
  53. Response.Write("<script>alert('請選擇正確的文件類型!')</script>");  
  54.  
  55. return null;  
  56.  
  57. }  
  58.  
  59. ///上傳文件已經(jīng)實(shí)現(xiàn)、現(xiàn)在需要將XML文檔中的數(shù)據(jù)進(jìn)行修改。  
  60.  
  61. return FileName;  
  62.  

 

技術(shù)節(jié)點(diǎn)二:動(dòng)態(tài)的對XML文檔進(jìn)行處理

 

  1. /// <summary> 
  2.  
  3. ///現(xiàn)在將數(shù)據(jù)存放到XML文檔中去。  
  4.  
  5. /// </summary> 
  6.  
  7. private void SavaFileXml() {  
  8.  
  9. string test =this.UpImg();  
  10.  
  11. if(!string.IsNullOrEmpty(test)){  
  12.  
  13. string imagepath = "~/newsimages/" + test;  
  14.  
  15. ///同時(shí)將這些數(shù)據(jù)寫入到數(shù)據(jù)庫中  
  16.  
  17. ///  
  18.  
  19. InputData(imagepath);///保存到數(shù)據(jù)庫中去.  
  20.  
  21. UpdaXml(Server.MapPath("~/xml/bcastr.xml"));  
  22.  
  23. }  
  24.  

 

技術(shù)節(jié)點(diǎn)三:將數(shù)據(jù)同時(shí)添加到數(shù)據(jù)庫中

 

  1. /// <summary> 
  2.  
  3. /// 保存到數(shù)據(jù)庫中.  
  4.  
  5. /// </summary> 
  6.  
  7. /// <param name="ImgPath"></param> 
  8.  
  9. private void InputData(string ImgPath) {  
  10.  
  11. StringBuilder sql = new StringBuilder();  
  12.  
  13. sql.Append("insert into Tb_Img(Title,Auther,Stime,ImgPath,Contente)");  
  14.  
  15. sql.Append("values(@Title,@Author,@Pubdate,@ImagePath,@Content)");  
  16.  
  17. ///構(gòu)造參數(shù)  
  18.  
  19. ///  
  20.  
  21. string sqlcon = "initial catalog=FileUpData;server=(local);integrated security=true;";  
  22.  
  23. SqlParameter[] para = new SqlParameter[] {new SqlParameter("@Title",tx_Title.Text),  
  24.  
  25. new SqlParameter("@Author",tx_author.Text),  
  26.  
  27. new SqlParameter("@Pubdate",DateTime.Now.ToString()),  
  28.  
  29. new SqlParameter("@ImagePath",ImgPath),  
  30.  
  31. new SqlParameter("@Content",tx_content.Text)  
  32.  
  33. };  
  34.  
  35. SqlConnection sqlconn = new SqlConnection();  
  36.  
  37. sqlconsqlconn.ConnectionString = sqlcon;  
  38.  
  39. sqlconn.Open();  
  40.  
  41. SqlCommand sqlcmd = new SqlCommand(sql.ToString(),sqlconn);  
  42.  
  43. sqlcmd.Parameters.AddRange(para);  
  44.  
  45. sqlcmd.ExecuteNonQuery();  
  46.  
  47. sqlconn.Close();///關(guān)閉數(shù)據(jù)庫.  
  48.  
  49. ///上面的代碼稍微有點(diǎn)問題.  
  50.  
  51. ///  
  52.  
  53. //string sql = "insert into Tb_Img(Title,Auther,Stime,ImgPath,Contente) values('"+tx_Title.Text+"','"+tx_author.Text+"','"+DateTime.Now.ToString()+"','"+ImgPath+"','"+tx_content.Text+"')";  
  54.  
  55. //SqlConnection sqlconn = new SqlConnection(sqlcon);  
  56.  
  57. //sqlconn.Open();  
  58.  
  59. //SqlCommand sqlcmd = new SqlCommand(sql, sqlconn);  
  60.  
  61. //sqlcmd.CommandType = CommandType.Text;  
  62.  
  63. //sqlcmd.ExecuteNonQuery();  
  64.  
  65. //sqlconn.Close();  
  66.  

 

技術(shù)節(jié)點(diǎn)四:將讀取出來的數(shù)據(jù)存放到XML文檔中去

將同時(shí)讀取出來的是四條數(shù)據(jù),一起存放到XML文檔中:

 

 

  1. /// <summary> 
  2.  
  3. /// 讀取數(shù)據(jù)庫中的最前面的四條數(shù)據(jù);  
  4.  
  5. /// </summary> 
  6.  
  7. private DataTable GetDataImg() {  
  8.  
  9. string sqlcon = "initial catalog=FileUpData;server=(local);integrated security=true;";///連接數(shù)據(jù)庫  
  10.  
  11. string sql = "SELECT top 4 ID,ImgPath  FROM Tb_Img  order by ID desc ";  
  12.  
  13. SqlConnection sqlconn = new SqlConnection(sqlcon);  
  14.  
  15. //sqlconn.Open();  
  16.  
  17. SqlCommand sqlcmd = new SqlCommand(sql, sqlconn);  
  18.  
  19. SqlDataAdapter da = new SqlDataAdapter(sqlcmd);  
  20.  
  21. DataSet ds = new DataSet();  
  22.  
  23. da.Fill(ds);  
  24.  
  25. return ds.Tables[0];///返回一張數(shù)據(jù)表.  
  26.  
  27. }  
  28.  
  29. /// <summary> 
  30.  
  31. /// 更改發(fā)XML文檔中的四條數(shù)據(jù)  
  32.  
  33. /// </summary> 
  34.  
  35. private void UpdaXml(string XmlPath) {///指定XML文檔的路徑  
  36.  
  37. DataTable dt = GetDataImg();///返回的是一個(gè)數(shù)據(jù)表.  
  38.  
  39. XmlDocument doc = new XmlDocument();  
  40.  
  41. doc.Load(XmlPath);///加載  
  42.  
  43. ///  
  44.  
  45. ///獲取根節(jié)點(diǎn)  
  46.  
  47. ///  
  48.  
  49. XmlElement root = doc.DocumentElement;  
  50.  
  51. ///清除根節(jié)點(diǎn)的所有節(jié)點(diǎn),新添內(nèi)容到文檔中去.  
  52.  
  53. ///  
  54.  
  55. root.RemoveAll();///移除所有節(jié)點(diǎn).  
  56.  
  57.  
  58. ///  
  59.  
  60. ///新添內(nèi)容  
  61.  
  62. for (int i = 0; i < dt.Rows.Count;i++ )  
  63.  
  64. {  
  65.  
  66. ///創(chuàng)建節(jié)點(diǎn)  
  67.  
  68. ///  
  69.  
  70. XmlElement newitem = doc.CreateElement("item");  
  71.  
  72. newitem.SetAttribute("item_url",dt.Rows[i][1].ToString());  
  73.  
  74. newitem.SetAttribute("link","show.aspx?ID="+dt.Rows[i][0].ToString());  
  75.  
  76. ///創(chuàng)建的節(jié)點(diǎn)屬性有:item_url,show.aspx?ID;共兩個(gè).  
  77.  
  78. ///  
  79.  
  80. root.AppendChild(newitem);  
  81.  
  82. }  
  83.  
  84. doc.Save(XmlPath);  
  85.  
  86. Response.Write("<script>alert('成功了!加油,好野狼!!')</script>");  
  87.  

 

關(guān)于C#對XML文檔和數(shù)據(jù)庫操作的四個(gè)技術(shù)節(jié)點(diǎn)就介紹到這里了,希望本次的介紹能夠給您帶來一些收獲!

【編輯推薦】

  1. Oracle數(shù)據(jù)庫如何創(chuàng)建虛擬列和復(fù)合觸發(fā)器
  2. MySQL數(shù)據(jù)庫目錄結(jié)構(gòu)及常用命令的使用總結(jié)篇
  3. 以MySQL為例介紹數(shù)據(jù)庫測試工具dbmonster的使用
  4. Oracle基于用戶管理的備份與恢復(fù)之歸檔日志和參數(shù)文件
  5. Oracle數(shù)據(jù)庫基于用戶管理的備份與恢復(fù)之恢復(fù)重做日志
責(zé)任編輯:趙鵬 來源: CSDN博客
相關(guān)推薦

2011-08-22 10:32:32

SQL Server數(shù)XML節(jié)點(diǎn)XML塊

2024-04-03 00:06:03

2024-04-18 09:56:16

2023-10-26 07:05:58

MySQL數(shù)據(jù)庫

2009-03-03 11:41:14

數(shù)據(jù)庫表表分區(qū)

2009-08-17 17:42:57

C#數(shù)據(jù)庫操作類

2021-10-26 22:43:05

數(shù)據(jù)庫安全存儲(chǔ)

2009-09-04 14:14:55

C#文檔

2009-08-18 17:08:50

C#編寫XML文檔

2009-08-24 17:24:28

C#創(chuàng)建XML文檔

2021-08-26 15:27:29

數(shù)據(jù)庫服務(wù)器數(shù)據(jù)庫服務(wù)器

2022-07-30 07:50:40

數(shù)據(jù)庫字段存儲(chǔ)

2009-08-19 16:30:55

C#操作Access數(shù)

2009-08-10 18:05:19

C#數(shù)據(jù)庫查詢

2022-03-29 15:17:51

數(shù)據(jù)安全網(wǎng)絡(luò)安全

2009-08-19 16:40:26

C#操作Access數(shù)

2021-03-08 15:42:54

數(shù)據(jù)庫Apache Iceb開源

2009-08-12 16:26:30

C#讀取XML文檔

2011-04-01 10:26:28

SQL Server 數(shù)據(jù)庫鏡像

2023-02-06 16:33:33

流式數(shù)據(jù)庫數(shù)據(jù)庫
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)