当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 使用函数传递参数来执行相应的数据库操作

ASP.NET
部署ASP.NET的三大技术(2)
部署ASP.NET的三大技术(3)
部署ASP.NET的三大技术(4)
部署ASP.NET的三大技术(5)
部署ASP.NET的三大技术(6)
ASP.NET可交互式位图窗体设计(1)
ASP.NET可交互式位图窗体设计(2)
ASP.NET可交互式位图窗体设计(3)
ASP.NET可交互式位图窗体设计(4)
ASP.NET可交互式位图窗体设计(5)
ASP.NET可交互式位图窗体设计(6)
ASP.NET可交互式位图窗体设计(7)
ASP.NET可交互式位图窗体设计(8)
ASP.NET可交互式位图窗体设计(9)
ADO.NET2.0的十大新特性
用Asp.net实现基于XML的留言簿之一
用Asp.net实现基于XML的留言簿之二
用Asp.net实现基于XML的留言簿之三
用Asp.net实现基于XML的留言簿之四
在ASP.NET中使用.NET组件

ASP.NET 中的 使用函数传递参数来执行相应的数据库操作


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 237 ::
收藏到网摘: n/a

using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
比如:
// 打开数据库

转自:动态网制作指南 www.knowsky.com
public static SqlConnection OpenConnection()
  {
   SqlConnection mysqlConn = new SqlConnection(ConfigurationSettings.AppSettings["connstring"]);
  

   try
   {
    mysqlConn.Open();
   }
   catch (Exception e)
   {
    //rethrow this exception
    throw e;
   }

   return mysqlConn;
  }

// 执行SQL返回DataSet
public static DataSet GetDataSet(string SQLQuery)
  {
   SqlConnection cn = DBObject.OpenConnection();
   SqlDataAdapter da = new SqlDataAdapter(SQLQuery, cn);
   DataSet ds = new DataSet();
  
   da.Fill(ds);
  
   //release resources
   da.Dispose();
   da = null;
   cn.Close();
   cn = null;

   return ds;
  }

// 执行SQL语句
public static void ExecuteUpdateQuery(string SQLQuery)
  {
   SqlConnection cn = DBObject.OpenConnection();
   SqlCommand cmd = new SqlCommand();

   cmd.CommandText = SQLQuery;
   cmd.CommandType = CommandType.Text;
   cmd.Connection = cn;
   cmd.ExecuteNonQuery();
  
   cn.Close();
   cn = null;
  }