¹ØÓÚNamespace(ÃüÃû¿Õ¼ä)µÄʹÓà ÔÚÇ°ÃæµÄ³ÌÐòÖÐÎÒÃÇ¿´µ½£¬ÎÒ³£ÓÃ<% @ Import Namespace="System.Data" %>,ÕâÊÇÔÚÒýÓÃM$ΪÎÒÃÇÌṩµÄNamespace,ÕâºÍASP²»Í¬µÄ£¬ÎÒÃÇÔÚASP.net±ØÐëÏÈÒýÓÃÓëÎÒÃDzÙ×÷ÓйصÄNamespaceºó²ÅÄÜʹÓÃÏàÓ¦µÄ¹¦ÄÜ¡£Æäʵ˵°×ÁË£¬Ò»¸öNamespace; ¾ÍÊÇÒ»¸ö×é¼þ¡£Õâ¸öÊǹØÓÚASP.netµÄ¸ß¼¶Ó¦Óã¬ÎÒ»áÔÚºóÃæµÄÕ½ڽ²¡££¨²»¹ýҪдµ½ÄÇÀʱ¼ä......) ÎÒÏÂÃæ¼òµ¥µÄÁоÙһЩ³£ÓõÄNamespace <% @ Import Namespace="System.Data" %> ´¦ÀíÊý¾ÝʱÓõ½ <% @ Import Namespace="System.Data.ADO" % > ʹÓÃADO.net ; ʱÓõ½ <% @ Import Namespace="System.Data.SQL" %> SQL Server Êý¾Ý¿âרÓà <% @ Import Namespace="System.Data.XML" %> ²»Óÿ´´¦ÀíXMLÓõ½ <% @ Import Namespace="System.IO" %> ´¦ÀíÎļþʱÓõ½ <% @ Import Namespace="System.Web.Util" %> ·¢Óʼþʱ´ó¼Ò»áÓõ½ <% @ Import Namespace="System.Text" %> Îı¾±àÂëʱÓõ½ ²Ù×÷Êý¾Ý¿âÐèÒªµÄ¶«¶« ½²½âÁËNamespace,ÎÒÃÇ¿ÉÒÔÕýʽÀ´ÌÖÂÛÊý¾Ý¿âµÄÓ¦ÓÃÁË¡£´ÓÉÏÃæµÄ¿ÉÒÔ¿´³ö£¬ÎÒÃDzÙ×÷Êý¾Ý¿â£¬ÎÒÃÇÐèÒªÒýÓÃÏÂÃæÁ½¸öNamespace <% @ Import Namespace="System.Data" %> <% @ Import Namespace="System.Data.SQL" %> ÆäʵSystem.Data.SQL ¿ÉÒÔÓÃSystem.Data.ADOÀ´´úÌ棬SQLÊÇSQL ServerרÓã¬ADO¿ÉÒÔÖ§³ÖÈκÎÊý¾Ý¿â£¨Ö»ÒªÔÚÖ÷»úÉÏ´æÔÚÏàÓ¦µÄÇý¶¯¾ÍÐÐÁË,ÈçAccess,Mysql,OracleÖ®ÀàµÄ£©,ÕâÀïÓÉÓڷɵ¶µÄÊý¾Ý¿âÊÇSQL Server,±¾À´¿ÉÒÔÓÃADO£¬µ«ÊÇÏëÏëM$µ¥¶À°ÑSQL¶ÀÁ¢³öÀ´£¬ÎªºÎ²»ÓÃÄØ¡£ÖÁÓÚËüÄÜ´øÀ´¶àÉٺô¦£¬·Éµ¶Ã»ÓвâÊÔ¹ý£¬¶ÔSQL Server¿Ï¶¨Òª±ÈADOºÃÒ»µãÀ²¡£ ÎÞÂÛÊÇADO»¹ÊÇSQL £¬ËûÃǶ¼Óм¸¸ö»ù±¾µÄ¶ÔÏóÓÃÓÚ²Ù×÷ Connections Á¬½áµ½Ò»¸öÊý¾Ý¿â£¬ÒÔ±ãÓÚºóÃæµÄÓ¦Óã¨ÀàËÆADOÖеÄConnections) Commands Ö´ÐÐSQLÓï¾äµÄµØ·½ DataReader ¶ÁÈ¡Ö´Ðк󷵻صÄÊý¾ÝÄÚÈÝ DataSet ´¢´æÊý¾Ý£¬¹¦ÄÜÇ¿´ó£¬ÎÒÃÇ»á¾ßÌå½²½â DataSetCommand Ö´ÐÐSQLÓï¾ä,²¢°ÑÊý¾Ý´æÈëDataSet ÕâÀïÃæ¿ÉÄÜ×îÄÑÀí½âµÄ¾ÍÊÇDataSet,ÎÒÃÇÏȲ»È¥¹ÜËû£¬ÏÈÄÃÈíµÄ¿ªµ¶ Connections(SQLConection »òÕß ADOConnection) ËüµÄÖ÷ÒªÈÎÎñ¾ÍÊǽ¨Á¢Ò»¸öÓëÊý¾Ý¿â·þÎñÆ÷µÄÁª½á <% @ Page Language="C#" %> <% @ Import Namespace="System.Data" %> <% @ Import Namespace="System.Data.SQL" %> <Script Language= "C#" Runat= "Server"> public void Page_Load(Object src,EventArgs e) { stringstrProvider="server=localhost;uid=sa;pwd=;database=aspcn"; SQLConnection MyConnection=new SQLConnection(strProvider); } </script> ÉÏÃæÎÒÃǽ¨Á¢ÁËÒ»¸öÃûΪMyConnectionµÄÁª½á£¬¾ÍºÃÏñÎÒÃÇÔÚASPÖÐÓÃADODB.Connection´ò¿ªÁËÒ»¸öÁª½á.Õâ¸öÁª½áÎÒÃÇÔÚCommand»òÕßDataSetCommandÖн«»áʹÓÃ. ËüµÄһЩÓÐÓõÄÊôÐԺͷ½·¨ÓÐ ConnectionString È¡µÃ»òÉèÖÃÁ¬½áÊý¾Ý¿âµÄÓï¾ä ConnectionTimeout È¡µÃ»òÉèÖÃÁ¬½áÊý¾Ý¿âµÄ×ʱ¼ä£¬Ò²ÊǾͳ¬Ê±Ê±¼ä DataBase È¡µÃ»òÉèÖÃÔÚÊý¾Ý¿â·þÎñÆ÷ÉÏÒª´ò¿ªµÄÊý¾Ý¿âÃû DataSource È¡µÃ»òÉèÖÃDSN,´ó¼Ò²»»áÄ°Éú°É£º£© Password È¡µÃ»òÉèÖÃÃÜÂë UserID È¡µÃ»òÉèÖõǽÃû State È¡µÃÄ¿Ç°Áª½áµÄ״̬ Open() ´ò¿ªÁª½á Close() ¹Ø±ÕÁª½á Clone() ¿Ë¡һ¸öÁª½á¡££¨ºÇºÇ£¬ÃàÑò¿ÉÒÔConnectionÎÒÒ²¿ÉÒÔ) ÎÒÃÇҲͨ¹ýÒ»¸öСÀý×ÓÀ´¿´¿´ËûÃǵÄÓ÷¨£º SQLConnection myConnection = new SQLConnection(); myConnection.DataSource = "mySQLServer"; myConnection.Password = ""; myConnection.UserID = "sa"; myConnection.ConnectionTimeout = 30; myConnection.Open(); myConnection.Database = "northwind"; myConnection.IsolationLevel = IsolationLevel.ReadCommitted Commands(SQLCommand »òÕß ADOCommand) ÉÏÃæµÄ³ÌÐòÖÐÎÒÃÇ´ò¿ªÁËÒ»¸öÁª½á£¬ÕâÀïÎÒÃǾÍÐèÒªÀ´Ê¹ÓÃÕâ¸ö,¿´Àý×ӱȽϺÃ: <% @ Page Language="C#" %> <% @ Import Namespace="System.Data" %> <% @ Import Namespace="System.Data.SQL" %> <Script Language="C#" Runat="Server"> public void Page_Load(Object src,EventArgs e) { stringstrProvider="server=localhost;uid=sa;pwd=;database=aspcn"; string strIndex="select * from aspcn where purview='webmaster'"; SQLConnection MyConnection=new SQLConnection(strProvider); SQLCommand MyCommand = new SQLCommand(strIndex,MyConnection); MyConnection.Open(); //´ò¿ªÁª½á MyCommand.ExecuteNonQuery(); //Ö´ÐÐSQL£¬µ«²»·µ»ØÈκμǼ MyConnection.Close(); } </script> ÔÚÉÏÃæµÄÀý×ÓÖÐÎÒÃǽ¨Á¢SQLCommand¶ÔÏóʱÒýÓÃÁËÁ½¸ö²ÎÊý(strIndex,MyConnection),´ÓÔ´³ÌÐòÖÐÎÒÃÇÒ²¿ÉÒÔ¿´³öÀ´strIndex´ú±íµÄÊÇÖ´ÐеÄSQLÓï¾ä£¬MyConnectionÊÇÎÒÃÇÏÈÇ°½¨Á¢µÄÁª½á.È»ºóÎÒÃǾÍÒªÏÈ´ò¿ªMyConnnection,È»ºóÔÙÖ´ÐÐÕâ¸öSQLÓï¾ä¡£ÎÒÃÇÔÚÕâÀïÖ´ÐÐÓõÄÊÇExecuteNonQuery()·½·¨£¬ÕâÑù²»·µ»Ø¼Ç¼¼¯£¬Ö»ÊÇ·µ»ØÊÜÓ°ÏìµÄ¼Ç¼¸öÊý¡£ ÕâÀïÎÒÃÇ´ò¿ªºÍ¹Ø±ÕÊý¾Ý¿âÒ²¿ÉÒÔÕâÑù×ö¡£ stringstrProvider="server=localhost;uid=sa;pwd=;database=aspcn"; string strIndex="select * from aspcn where purview='webmaster'"; SQLConnection MyConnection=new SQLConnection(strProvider); SQLCommand MyCommand = new SQLCommand(strIndex,MyConnection); MyCommand.ActiveConnection.Open(); MyCommand.ExecuteNonQuery(); MyCommand.ActiveConnection.Close(); ËùµÃ½á¹ûºÍÏÈÇ°µÄÒ»Ñù¡£ËùÒÔÖ´ÐÐÒ»ÌõSQLÓï¾äÓкܶàÖÖ·½·¨¡£¶øÇÒ»¹²»Ö»Á½ÖÖ£¬ÎÒÃǺóÃæѧÁËDataSetCommand,ÄÇ´ò¿ª·½·¨¾ÍÊÇNÖÖÁË£º£©Õâ¾ÍÐèÒª¿´ÄãµÄÏ°¹ßºÍ³ÌÐòµÄÒªÇóÁË£»£© ÎÒÃÇÏÈÀ´¿´¿´Command³£Óõķ½·¨ºÍÊôÐÔ ActiveConnection È¡µÃ»òÉèÖÃÁª½áConnections CommandText Ö´ÐеÄSQLÓï¾ä»ò´¢´æ¹ý³Ì(StoredProcedure)Ãû CommandTimeout Ö´ÐеÄ×ʱ¼ä CommandType Command²Ù×÷µÄÀàÐÍ(StoredProcedure,Text,TableDirect)ÈýÖÖ,ĬÈÏText Parameters ²Ù×÷´¢´æ¹ý³ÌʱʹÓà Execute() Ö´ÐÐSQLÓï¾ä»ò´¢´æ¹ý³Ì ExecuteNonQuery() ͬÉÏ£¬Çø±ðÔÚÓÚ²»·µ»Ø¼Ç¼¼¯ Clone() ¿Ë¡Command ͬÑù¿´Ò»¸öÀý×Ó£º string mySelectQuery = "SELECT * FROM Categories ORDER BY CategoryID"; stringmyConnectString="userid=sa;password=;database=northwind;server=mySQLServer"; SQLCommand myCommand = new SQLCommand(mySelectQuery); myCommand.ActiveConnection = new SQLConnection(myConnectString); myCommand.CommandTimeout = 15; myCommand.CommandType = CommandType.Text;< /FONT > |