利用C#對XML文檔和數(shù)據(jù)庫操作的四個(gè)技術(shù)節(jié)點(diǎn)
作者:krenyelang
本文我們主要介紹了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í)行:
- protected void Button2_Click(object sender, EventArgs e)
- {
- SavaFileXml(); ///通過調(diào)用這個(gè)方法,然后再調(diào)用其他的方法,進(jìn)而實(shí)現(xiàn)所要求實(shí)現(xiàn)的功能。
- }
接下來我們就來介紹這些技術(shù)。
技術(shù)節(jié)點(diǎn)一:將圖像上傳到服務(wù)器上面
- private string UpImg() {
- ///將圖像上傳到服務(wù)器中去。
- ///
- if (this.FileUP.HasFile == false)
- {///判斷是否為空
- Label1.Text = "請選擇要上傳的圖像";
- //Response.Write("<script>alert('請選擇要上傳的文件!')</script>");
- return null;
- }
- ///獲取圖像
- string FileName = this.FileUP.FileName;
- ///獲取圖像的類型.
- //string FileType = FileName.Substring(FileName.LastIndexOf('.')+1);
- string fType = this.FileUP.PostedFile.ContentType;
- if (fType == "image/bmp" || fType == "image/gif" || fType == "image/pjpeg" || fType == "image/x-png")
- {
- if(File.Exists(Server.MapPath("~/newsimages/" + FileName))==true){///判斷該圖像是否已經(jīng)存在了.
- Label1.Text = "該圖像已經(jīng)存在了!";
- return null;
- }
- this.FileUP.PostedFile.SaveAs(Server.MapPath("~/newsimages/" + FileName));
- Response.Write("<script>alert('正確了!')</script>");
- //return FileName;///返回圖像名稱
- }
- else
- {
- Response.Write("<script>alert('請選擇正確的文件類型!')</script>");
- return null;
- }
- ///上傳文件已經(jīng)實(shí)現(xiàn)、現(xiàn)在需要將XML文檔中的數(shù)據(jù)進(jìn)行修改。
- return FileName;
- }
技術(shù)節(jié)點(diǎn)二:動(dòng)態(tài)的對XML文檔進(jìn)行處理
- /// <summary>
- ///現(xiàn)在將數(shù)據(jù)存放到XML文檔中去。
- /// </summary>
- private void SavaFileXml() {
- string test =this.UpImg();
- if(!string.IsNullOrEmpty(test)){
- string imagepath = "~/newsimages/" + test;
- ///同時(shí)將這些數(shù)據(jù)寫入到數(shù)據(jù)庫中
- ///
- InputData(imagepath);///保存到數(shù)據(jù)庫中去.
- UpdaXml(Server.MapPath("~/xml/bcastr.xml"));
- }
- }
技術(shù)節(jié)點(diǎn)三:將數(shù)據(jù)同時(shí)添加到數(shù)據(jù)庫中
- /// <summary>
- /// 保存到數(shù)據(jù)庫中.
- /// </summary>
- /// <param name="ImgPath"></param>
- private void InputData(string ImgPath) {
- StringBuilder sql = new StringBuilder();
- sql.Append("insert into Tb_Img(Title,Auther,Stime,ImgPath,Contente)");
- sql.Append("values(@Title,@Author,@Pubdate,@ImagePath,@Content)");
- ///構(gòu)造參數(shù)
- ///
- string sqlcon = "initial catalog=FileUpData;server=(local);integrated security=true;";
- SqlParameter[] para = new SqlParameter[] {new SqlParameter("@Title",tx_Title.Text),
- new SqlParameter("@Author",tx_author.Text),
- new SqlParameter("@Pubdate",DateTime.Now.ToString()),
- new SqlParameter("@ImagePath",ImgPath),
- new SqlParameter("@Content",tx_content.Text)
- };
- SqlConnection sqlconn = new SqlConnection();
- sqlconsqlconn.ConnectionString = sqlcon;
- sqlconn.Open();
- SqlCommand sqlcmd = new SqlCommand(sql.ToString(),sqlconn);
- sqlcmd.Parameters.AddRange(para);
- sqlcmd.ExecuteNonQuery();
- sqlconn.Close();///關(guān)閉數(shù)據(jù)庫.
- ///上面的代碼稍微有點(diǎn)問題.
- ///
- //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+"')";
- //SqlConnection sqlconn = new SqlConnection(sqlcon);
- //sqlconn.Open();
- //SqlCommand sqlcmd = new SqlCommand(sql, sqlconn);
- //sqlcmd.CommandType = CommandType.Text;
- //sqlcmd.ExecuteNonQuery();
- //sqlconn.Close();
- }
技術(shù)節(jié)點(diǎn)四:將讀取出來的數(shù)據(jù)存放到XML文檔中去
將同時(shí)讀取出來的是四條數(shù)據(jù),一起存放到XML文檔中:
- /// <summary>
- /// 讀取數(shù)據(jù)庫中的最前面的四條數(shù)據(jù);
- /// </summary>
- private DataTable GetDataImg() {
- string sqlcon = "initial catalog=FileUpData;server=(local);integrated security=true;";///連接數(shù)據(jù)庫
- string sql = "SELECT top 4 ID,ImgPath FROM Tb_Img order by ID desc ";
- SqlConnection sqlconn = new SqlConnection(sqlcon);
- //sqlconn.Open();
- SqlCommand sqlcmd = new SqlCommand(sql, sqlconn);
- SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- return ds.Tables[0];///返回一張數(shù)據(jù)表.
- }
- /// <summary>
- /// 更改發(fā)XML文檔中的四條數(shù)據(jù)
- /// </summary>
- private void UpdaXml(string XmlPath) {///指定XML文檔的路徑
- DataTable dt = GetDataImg();///返回的是一個(gè)數(shù)據(jù)表.
- XmlDocument doc = new XmlDocument();
- doc.Load(XmlPath);///加載
- ///
- ///獲取根節(jié)點(diǎn)
- ///
- XmlElement root = doc.DocumentElement;
- ///清除根節(jié)點(diǎn)的所有節(jié)點(diǎn),新添內(nèi)容到文檔中去.
- ///
- root.RemoveAll();///移除所有節(jié)點(diǎn).
- ///
- ///新添內(nèi)容
- for (int i = 0; i < dt.Rows.Count;i++ )
- {
- ///創(chuàng)建節(jié)點(diǎn)
- ///
- XmlElement newitem = doc.CreateElement("item");
- newitem.SetAttribute("item_url",dt.Rows[i][1].ToString());
- newitem.SetAttribute("link","show.aspx?ID="+dt.Rows[i][0].ToString());
- ///創(chuàng)建的節(jié)點(diǎn)屬性有:item_url,show.aspx?ID;共兩個(gè).
- ///
- root.AppendChild(newitem);
- }
- doc.Save(XmlPath);
- Response.Write("<script>alert('成功了!加油,好野狼!!')</script>");
- }
關(guān)于C#對XML文檔和數(shù)據(jù)庫操作的四個(gè)技術(shù)節(jié)點(diǎn)就介紹到這里了,希望本次的介紹能夠給您帶來一些收獲!
【編輯推薦】
責(zé)任編輯:趙鵬
來源:
CSDN博客


相關(guān)推薦




