ASP.Net 连接ACCESS查询?
Asp.Net连接ACCESS我知道连接字符串!但是以前做的时候是用SQLServer连接的,现在要怎样做才能把连接改为Access?命名空间:usingSystem.D...
Asp.Net连接ACCESS 我知道连接字符串!但是以前做的时候是用SQLServer 连接的,现在要怎样做才能把连接改为Access?
命名空间:
using System.Data;
using System.Data.SqlClient;
连接字符串:
private static string conStr = @"Data Source=NEWARE-PC\SQLEXPRESS;Initial Catalog=db1;Integrated Security=True";//连接字符串
方法:
public static DataTable GetImagePath(string sqlText)
{
SqlConnection con = new SqlConnection(conStr);
con.Open();
SqlCommand cmd = new SqlCommand(sqlText, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
求高手指导下:数据库名字:db1.mdf 展开
命名空间:
using System.Data;
using System.Data.SqlClient;
连接字符串:
private static string conStr = @"Data Source=NEWARE-PC\SQLEXPRESS;Initial Catalog=db1;Integrated Security=True";//连接字符串
方法:
public static DataTable GetImagePath(string sqlText)
{
SqlConnection con = new SqlConnection(conStr);
con.Open();
SqlCommand cmd = new SqlCommand(sqlText, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
求高手指导下:数据库名字:db1.mdf 展开
2个回答
展开全部
首先不能写绝对路径。
在webconfig中配置
<add key="AccessStrings" value="provider=microsoft.jet.oledb.4.0;data source="/>
<add key="dbPath" value="~/文件夹/db1.mdf "/>
这样写的好处,不管项目放在那个盘下都会读取到。
然后在获取链接字符串,这个相对路径的字符串是拼接出来的。
string connstring = System.Configuration.ConfigurationManager.AppSettings["AccessStrings"].ToString() + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"]).ToString()+";" ;
完整代码、
public class ACHelper
{
public ACHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//定义全局变量
private static OleDbConnection conn;
private static OleDbCommand cmd;
private static OleDbDataReader dr;
private static OleDbDataAdapter adapter;
//数据库连接
public static OleDbConnection connectionstrings
{
get
{
string connstring = System.Configuration.ConfigurationManager.AppSettings["AccessStrings"].ToString() + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"]).ToString()+";" ;
conn = new OleDbConnection(connstring);
conn.Open();
return conn;
}
}
/// <summary>
/// 单向操作,主要用于(增加,删除,修改),返回受影响的行数
/// </summary>
/// <param name="cmdTxt">安全的sql语句(string.format)</param>
/// <returns></returns>
public static int GetExcuteNonQuery(string cmdTxt)
{
try
{
return GetExcuteNonQuery(cmdTxt, null);
}
finally
{
conn.Close();
}
}
/// <summary>
/// 带参数化的 主要用于(增加,删除,修改),返回受影响的行数
/// </summary>
/// <param name="cmdTxt"></param>
/// <param name="pars"></param>
/// <returns></returns>
public static int GetExcuteNonQuery(string cmdTxt, params OleDbParameter[] pars)
{
using (cmd = new OleDbCommand(cmdTxt, connectionstrings))
{
if (pars != null)
cmd.Parameters.AddRange(pars);
return cmd.ExecuteNonQuery();
}
}
/// <summary>
/// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
/// </summary>
/// <typeparam name="T">返回的类型</typeparam>
/// <param name="cmdText">SQL 语句</param>
/// <param name="pars">参数</param>
/// <returns>结果集中第一行的第一列或空引用</returns>
public static T ExecuteScalar<T>(string cmdText, params OleDbParameter[] pars)
{
using (OleDbCommand cmd = new OleDbCommand(cmdText, connectionstrings))
{
if (pars != null)
cmd.Parameters.AddRange(pars);
T result = (T)cmd.ExecuteScalar();
conn.Close();
return result;
}
}
/// <summary>
/// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cmdText"></param>
/// <returns></returns>
public static T ExecuteScalar<T>(string cmdText)
{
return ExecuteScalar<T>(cmdText, null);
}
/// <summary>
/// 将 cmdText 发送到 System.Data.SqlClient.OleDbCommand.Connection,并使用 System.Data.CommandBehavior 值之一生成一个 DataReader
/// </summary>
/// <param name="cmdTxt">安全的sql语句(string.format)</param>
/// <returns>一个 DataReader 对象</returns>
public static OleDbDataReader GetDataReader(string cmdTxt)
{
return GetDataReader(cmdTxt, null);
}
/// <summary>
/// 将 cmdText 发送到 System.Data.SqlClient.OleDbCommand.Connection,并使用 System.Data.CommandBehavior 值之一生成一个 DataReader
/// </summary>
/// <param name="cmdTxt">安全的sql语句(string.format)</param>
/// <param name="pars">参数</param>
/// <returns>一个 DataReader 对象</returns>
public static OleDbDataReader GetDataReader(string cmdTxt, params OleDbParameter[] pars)
{
using (cmd = new OleDbCommand(cmdTxt, connectionstrings))
{
if (pars != null)
cmd.Parameters.AddRange(pars);
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
}
/// <summary>
/// 做数据绑定显示作用
/// </summary>
/// <param name="cmdTxt">sql语句</param>
/// <param name="tableName">要绑定显示的具体表名</param>
/// <returns></returns>
public static DataTable GetFillData(string cmdTxt, string tableName)
{
using (adapter = new OleDbDataAdapter(cmdTxt, connectionstrings))
{
try
{
DataSet ds = new DataSet();
adapter.Fill(ds, tableName);
return ds.Tables[tableName];
}
finally
{
conn.Close();
}
}
}
/// <summary>
/// 返回DataSet对象
/// </summary>
/// <param name="SQL">SQL语句</param>
/// <returns>DataSet</returns>
///
public static DataSet GetDataSet(string SQL)
{
DataSet DS = new DataSet();
string DataTableName = "Table1";
try
{
using (adapter = new OleDbDataAdapter(SQL, connectionstrings))
{
adapter.Fill(DS, DataTableName);
}
}
catch (Exception ex)
{
ex.ToString();
DS = null;
}
return (DS);
}
/// <summary>
///返回OleDbDataAdapter对象
/// </summary>
/// <param name="SQL">SQL语句</param>
/// <returns>OleDbDataAdapter:为null时,表示返回失败;</returns>
public static OleDbDataAdapter GetDataAdapter(string SQL)
{
try
{
adapter = new OleDbDataAdapter(SQL, conn);
return (adapter);
}
catch
{
adapter = null;
return (adapter);
}
}
}
保证完美。
在webconfig中配置
<add key="AccessStrings" value="provider=microsoft.jet.oledb.4.0;data source="/>
<add key="dbPath" value="~/文件夹/db1.mdf "/>
这样写的好处,不管项目放在那个盘下都会读取到。
然后在获取链接字符串,这个相对路径的字符串是拼接出来的。
string connstring = System.Configuration.ConfigurationManager.AppSettings["AccessStrings"].ToString() + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"]).ToString()+";" ;
完整代码、
public class ACHelper
{
public ACHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//定义全局变量
private static OleDbConnection conn;
private static OleDbCommand cmd;
private static OleDbDataReader dr;
private static OleDbDataAdapter adapter;
//数据库连接
public static OleDbConnection connectionstrings
{
get
{
string connstring = System.Configuration.ConfigurationManager.AppSettings["AccessStrings"].ToString() + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"]).ToString()+";" ;
conn = new OleDbConnection(connstring);
conn.Open();
return conn;
}
}
/// <summary>
/// 单向操作,主要用于(增加,删除,修改),返回受影响的行数
/// </summary>
/// <param name="cmdTxt">安全的sql语句(string.format)</param>
/// <returns></returns>
public static int GetExcuteNonQuery(string cmdTxt)
{
try
{
return GetExcuteNonQuery(cmdTxt, null);
}
finally
{
conn.Close();
}
}
/// <summary>
/// 带参数化的 主要用于(增加,删除,修改),返回受影响的行数
/// </summary>
/// <param name="cmdTxt"></param>
/// <param name="pars"></param>
/// <returns></returns>
public static int GetExcuteNonQuery(string cmdTxt, params OleDbParameter[] pars)
{
using (cmd = new OleDbCommand(cmdTxt, connectionstrings))
{
if (pars != null)
cmd.Parameters.AddRange(pars);
return cmd.ExecuteNonQuery();
}
}
/// <summary>
/// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
/// </summary>
/// <typeparam name="T">返回的类型</typeparam>
/// <param name="cmdText">SQL 语句</param>
/// <param name="pars">参数</param>
/// <returns>结果集中第一行的第一列或空引用</returns>
public static T ExecuteScalar<T>(string cmdText, params OleDbParameter[] pars)
{
using (OleDbCommand cmd = new OleDbCommand(cmdText, connectionstrings))
{
if (pars != null)
cmd.Parameters.AddRange(pars);
T result = (T)cmd.ExecuteScalar();
conn.Close();
return result;
}
}
/// <summary>
/// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cmdText"></param>
/// <returns></returns>
public static T ExecuteScalar<T>(string cmdText)
{
return ExecuteScalar<T>(cmdText, null);
}
/// <summary>
/// 将 cmdText 发送到 System.Data.SqlClient.OleDbCommand.Connection,并使用 System.Data.CommandBehavior 值之一生成一个 DataReader
/// </summary>
/// <param name="cmdTxt">安全的sql语句(string.format)</param>
/// <returns>一个 DataReader 对象</returns>
public static OleDbDataReader GetDataReader(string cmdTxt)
{
return GetDataReader(cmdTxt, null);
}
/// <summary>
/// 将 cmdText 发送到 System.Data.SqlClient.OleDbCommand.Connection,并使用 System.Data.CommandBehavior 值之一生成一个 DataReader
/// </summary>
/// <param name="cmdTxt">安全的sql语句(string.format)</param>
/// <param name="pars">参数</param>
/// <returns>一个 DataReader 对象</returns>
public static OleDbDataReader GetDataReader(string cmdTxt, params OleDbParameter[] pars)
{
using (cmd = new OleDbCommand(cmdTxt, connectionstrings))
{
if (pars != null)
cmd.Parameters.AddRange(pars);
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
}
/// <summary>
/// 做数据绑定显示作用
/// </summary>
/// <param name="cmdTxt">sql语句</param>
/// <param name="tableName">要绑定显示的具体表名</param>
/// <returns></returns>
public static DataTable GetFillData(string cmdTxt, string tableName)
{
using (adapter = new OleDbDataAdapter(cmdTxt, connectionstrings))
{
try
{
DataSet ds = new DataSet();
adapter.Fill(ds, tableName);
return ds.Tables[tableName];
}
finally
{
conn.Close();
}
}
}
/// <summary>
/// 返回DataSet对象
/// </summary>
/// <param name="SQL">SQL语句</param>
/// <returns>DataSet</returns>
///
public static DataSet GetDataSet(string SQL)
{
DataSet DS = new DataSet();
string DataTableName = "Table1";
try
{
using (adapter = new OleDbDataAdapter(SQL, connectionstrings))
{
adapter.Fill(DS, DataTableName);
}
}
catch (Exception ex)
{
ex.ToString();
DS = null;
}
return (DS);
}
/// <summary>
///返回OleDbDataAdapter对象
/// </summary>
/// <param name="SQL">SQL语句</param>
/// <returns>OleDbDataAdapter:为null时,表示返回失败;</returns>
public static OleDbDataAdapter GetDataAdapter(string SQL)
{
try
{
adapter = new OleDbDataAdapter(SQL, conn);
return (adapter);
}
catch
{
adapter = null;
return (adapter);
}
}
}
保证完美。
展开全部
引用using System.Data.OleDb;而不是using System.Data.SqlClient
连接字符串string strconn="Provider=Microsoft.Jet.Oledb.4.0;data source=E:\db1.mdf";//E:\db1.mdf为数据库的物理路径
string strSql="select * from [table]"
使用OleDbConnection conn = new OleDbConnection(strconn)//连接
conn.Open();
OleDbCommand ODC = new OleDbCommand(strSql, conn);
OleDbDataReader ODDR = ODC.ExecuteReader();
while (ODDR.Read())
{//code}
conn.Close();
使用方法和Sql差不多,只是用的是Oledb
连接字符串string strconn="Provider=Microsoft.Jet.Oledb.4.0;data source=E:\db1.mdf";//E:\db1.mdf为数据库的物理路径
string strSql="select * from [table]"
使用OleDbConnection conn = new OleDbConnection(strconn)//连接
conn.Open();
OleDbCommand ODC = new OleDbCommand(strSql, conn);
OleDbDataReader ODDR = ODC.ExecuteReader();
while (ODDR.Read())
{//code}
conn.Close();
使用方法和Sql差不多,只是用的是Oledb
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询