RSS订阅优然探索
你的位置:首页 » 学习收藏 » 正文

C#技术学习之-SqlCommand

选择字号: 超大 标准 发布时间:2008-9-28 9:24:28 | 作者:admin | 0个评论 | 人浏览

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
namespace Mirror.AutoWeb.HelpCSharp.App_Code
{
 /// <summary>
 /// StudySQLCommand 的摘要说明。
 /// </summary>
 public class StudySQLCommand : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Literal Literal1;
  protected System.Web.UI.WebControls.Literal Literal3;
  protected System.Web.UI.WebControls.Literal Literal5;
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.Literal ScalarLabel;
  protected System.Web.UI.WebControls.Literal NonQueryLabel;
  private readonly string connstr = ConfigurationSettings.AppSettings["MySQLConnString1"].ToString();
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!Page.IsPostBack)
   {
    ScalarLabel.Text=ExecuteScalarMySqlCommand().ToString();
    ExecuteNoQueryMySqlCommand("10932 Bigge Rd.10932 Bigge Rd.");
    NonQueryLabel.Text = ExecuteScalarMySqlCommand().ToString();
    this.TextBox1.Text = ExecuteXmlReaderMySqlCommand().ToString();
   }
   
   
  }

  private object ExecuteScalarMySqlCommand()
  {
   string cmdText="Select * From authors";
   SqlConnection mySqlConnecton = new SqlConnection(connstr);
   SqlCommand myCommand = new SqlCommand(cmdText,mySqlConnecton);
    mySqlConnecton.Open();
   object scalarobject = myCommand.ExecuteScalar();
   mySqlConnecton.Close();
   return(scalarobject);
                 
  }
  private void ExecuteNoQueryMySqlCommand(string newName)
  {
   string cmdText = "update authors Set address='"+newName.ToString() +"' Where au_id='172-32-1176'";
   SqlConnection myConn = new SqlConnection(connstr);
   SqlCommand mycmd = new SqlCommand(cmdText.ToString(),myConn);
   myConn.Open();
   mycmd.ExecuteNonQuery();
   myConn.Close();
  }
  private string ExecuteXmlReaderMySqlCommand()
  {
   string cmdText = "Select * From authors FOR XML AUTO";
   SqlConnection myConn = new SqlConnection(connstr);
   SqlCommand myCmd = new SqlCommand(cmdText,myConn);
   myConn.Open();
   XmlReader xmlReader = myCmd.ExecuteXmlReader();
   string xmlString="";
   
   while(xmlReader.Read())
   {
    xmlString += xmlReader.ReadOuterXml() + "\n";
   }
   xmlReader.Close();
   myConn.Close();
   return xmlString;
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}
 

标签:SqlCommand  C#  

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。