asp.net 数据库连接代码(c#)完整代码
最好是ACCESS,我现在有一个数据库,就是不知道怎么和.net怎么连和对.net如何配置,请问哪为高手能帮我解决?...
最好是ACCESS,我现在有一个数据库,就是不知道怎么和.net怎么连和对.net如何配置,请问哪为高手能帮我解决?
展开
展开全部
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
namespace SQLFunction.DBHelper
{
public abstract class SqlHelper
{
public static readonly string CONN_STRING = ConfigurationManager.ConnectionStrings["SqlConnString"].ConnectionString;
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParams);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
public static int ExecuteNonQuery(SqlConnection conn, CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParams);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(CONN_STRING);
try
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParams);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParams);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
public static object ExecuteScalar(SqlConnection conn, CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParams);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
public static DataSet Fill(CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParams);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
}
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, CommandType cmdType, string cmdText, SqlParameter[] cmdParams)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandType = cmdType;
cmd.CommandText = cmdText;
if (cmdParams != null)
{
foreach (SqlParameter param in cmdParams)
cmd.Parameters.Add(param);
}
}
}
}
ASP.NET2.0的 读取配置文件中的数据库连接字符串"SqlConnString"
可能没看懂.这个封装可以直接引用. ExecuteNonQuery执行UPdate,DELETE 等SQL语句.返回影响多少记录,ExecuteReader执行一个查询语句返回SqlDataReader ,ExecuteScalar 执行执行SQL返回一个结果为object类型,Fill返回一个DATASET记录集,传递参数
(CommandType[TEXT/StoredProcedure],[SQL语句/存储过程名],[SqlParameter/没有则null])所以这个函数既可以执行存储过程也可用足普通SQL.
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
namespace SQLFunction.DBHelper
{
public abstract class SqlHelper
{
public static readonly string CONN_STRING = ConfigurationManager.ConnectionStrings["SqlConnString"].ConnectionString;
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParams);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
public static int ExecuteNonQuery(SqlConnection conn, CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParams);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(CONN_STRING);
try
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParams);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParams);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
public static object ExecuteScalar(SqlConnection conn, CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParams);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
public static DataSet Fill(CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(CONN_STRING))
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParams);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
}
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, CommandType cmdType, string cmdText, SqlParameter[] cmdParams)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandType = cmdType;
cmd.CommandText = cmdText;
if (cmdParams != null)
{
foreach (SqlParameter param in cmdParams)
cmd.Parameters.Add(param);
}
}
}
}
ASP.NET2.0的 读取配置文件中的数据库连接字符串"SqlConnString"
可能没看懂.这个封装可以直接引用. ExecuteNonQuery执行UPdate,DELETE 等SQL语句.返回影响多少记录,ExecuteReader执行一个查询语句返回SqlDataReader ,ExecuteScalar 执行执行SQL返回一个结果为object类型,Fill返回一个DATASET记录集,传递参数
(CommandType[TEXT/StoredProcedure],[SQL语句/存储过程名],[SqlParameter/没有则null])所以这个函数既可以执行存储过程也可用足普通SQL.
展开全部
SqlConnection conn = new SqlConnection("你的连接字符串");
SqlCommand cmm = new SqlCommand();
cmm.CommandText = "select * from 表 ";
cmm.Connection = conn;
conn.Open();
SqlDataReader dr = cmm.ExecuteReader();
dr.Read();
dr.Close();
conn.Close();
SqlCommand cmm = new SqlCommand();
cmm.CommandText = "select * from 表 ";
cmm.Connection = conn;
conn.Open();
SqlDataReader dr = cmm.ExecuteReader();
dr.Read();
dr.Close();
conn.Close();
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这是用存储过程的。
Connection="Data Source=服务器名或IP;UID=用户名;PWD=密码;DataBase=数据库名;
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["Connection"]);
SqlCommand cmd;
cmd = new SqlCommand("存储过程名", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("参数", SqlDbType.VarChar,50));
cmd.Parameters["@i_title"].Value = 要传的参数;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
cmd.Connection.Close();
cmd.Dispose();
Connection="Data Source=服务器名或IP;UID=用户名;PWD=密码;DataBase=数据库名;
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["Connection"]);
SqlCommand cmd;
cmd = new SqlCommand("存储过程名", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("参数", SqlDbType.VarChar,50));
cmd.Parameters["@i_title"].Value = 要传的参数;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
cmd.Connection.Close();
cmd.Dispose();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlConnection
conn
=
new
SqlConnection("你的连接字符串");
SqlCommand
cmm
=
new
SqlCommand();
cmm.CommandText
=
"select
*
from
表
";
cmm.Connection
=
conn;
conn.Open();
SqlDataReader
dr
=
cmm.ExecuteReader();
dr.Read();
dr.Close();
conn.Close();
conn
=
new
SqlConnection("你的连接字符串");
SqlCommand
cmm
=
new
SqlCommand();
cmm.CommandText
=
"select
*
from
表
";
cmm.Connection
=
conn;
conn.Open();
SqlDataReader
dr
=
cmm.ExecuteReader();
dr.Read();
dr.Close();
conn.Close();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询