首页 > 代码库 > asp web.config操作
asp web.config操作
//这是web.config配置的 sqlserver数据库连接设置
<configuration> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <connectionStrings> <add name="connStr" connectionString="Data Source=. ;Initial Catalog=test;User Id =sa;Password=123;"></add>//Initial Catalog=databaseName(数据库名字) </connectionStrings> </configuration>
下面的是sqlHelper类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.Data.SqlClient; using System.Data; namespace StudentMgr { public static class SqlHelper { public static readonly string connstr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; //新建连接对象 public static SqlConnection OpenConnection() { SqlConnection conn = new SqlConnection(connstr); conn.Open(); return conn; } public static int ExecuteNonQuery(string cmdText, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); return ExecuteNonQuery(conn, cmdText, parameters); } } //查询单个值 public static object ExecuteScalar(string cmdText, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); return ExecuteScalar(conn, cmdText, parameters); } } public static DataTable ExecuteDataTable(string cmdText, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); return ExecuteDataTable(conn, cmdText, parameters); } } public static int ExecuteNonQuery(SqlConnection conn,string cmdText, params SqlParameter[] parameters) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = cmdText; cmd.Parameters.AddRange(parameters); return cmd.ExecuteNonQuery(); } } public static object ExecuteScalar(SqlConnection conn, string cmdText, params SqlParameter[] parameters) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = cmdText; cmd.Parameters.AddRange(parameters); return cmd.ExecuteScalar(); } } public static DataTable ExecuteDataTable(SqlConnection conn, string cmdText, params SqlParameter[] parameters) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = cmdText; cmd.Parameters.AddRange(parameters); using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); adapter.Fill(dt); return dt; } } } public static object ToDBValue(this object value) { return value =http://www.mamicode.com/= null ? DBNull.Value : value; } public static object FromDBValue(this object dbValue) { return dbValue =http://www.mamicode.com/= DBNull.Value ? null : dbValue; } } }
asp web.config操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。