【新手求助】C#数据库操作类_带参数查询
请问写一个 带参数 更新的操作类,然后另外一个页面调用这个类来 更新数据库,这个类该怎么写?注:连接的是Access数据库
public DataTable Update( string strSQL, OleDbParameterCollection par)
{
}
之前写了几次...但老是出现参数传不进去...希望会的帮帮手,感激不尽!! --------------------编程问答-------------------- access的不玩了。不过基本和sqlserver一样。建议找一下sqlhelper类看看就知道了 --------------------编程问答-------------------- public class SQLHelper
{
private static string ConStr = ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString;
#region 增、删、改并返回影响行数
/// <summary>
/// 增、删、改并返回影响行数(只有SQL语句,参数、命令类型则默认为null,CommandType.Text)
/// </summary>
/// <param name="Sql">SQL语句</param>
/// <returns>执行结果</returns>
public static int ExecuteNonQuery(string Sql)
{
return ExecuteNonQuery(Sql, null, CommandType.Text);
}
/// <summary>
/// 增、删、改并返回影响行数(命令类型则默认CommandType.Text)
/// </summary>
/// <param name="Sql">SQL语句</param>
/// <param name="Prams">参数数组</param>
/// <returns>执行结果</returns>
public static int ExecuteNonQuery(string Sql, SqlParameter[] Prams)
{
return ExecuteNonQuery(Sql, Prams, CommandType.Text);
}
/// <summary>
/// 增、删、改并返回影响行数(含存储过程)
/// </summary>
/// <param name="Sql">SQL语句</param>
/// <param name="Prams">参数数组</param>
/// <param name="Type">命令类型</param>
/// <returns>执行结果</returns>
public static int ExecuteNonQuery(string Sql, SqlParameter[] Prams, CommandType Type)
{
int back = 0;
using (SqlConnection con = new SqlConnection(ConStr))
{
try
{
SqlCommand Command = new SqlCommand(Sql, con);
Command.CommandType = Type;
if (Prams != null)
Command.Parameters.AddRange(Prams);
con.Open();
back = Command.ExecuteNonQuery();
con.Close();
}
catch (Exception e)
{
string ex = e.Message;
}
}
return back;
}
#endregion
#region 增加数据并返回第自增列的值
/// <summary>
/// 增加数据并返回第自增列的值(只有SQL语句,参数、命令类型则默认为null,CommandType.Text)
/// </summary>
/// <param name="Sql">SQL语句</param>
/// <returns>执行结果</returns>
public static int ExecuteScalar(string Sql)
{
return ExecuteScalar(Sql, null, CommandType.Text);
}
/// <summary>
/// 增加数据并返回第自增列的值(命令类型则默认CommandType.Text)
/// </summary>
/// <param name="Sql">SQL语句</param>
/// <param name="Prams">参数数组</param>
/// <returns>执行结果</returns>
public static int ExecuteScalar(string Sql, SqlParameter[] Prams)
{
return ExecuteScalar(Sql, Prams, CommandType.Text);
}
/// <summary>
/// 增加数据并返回第自增列的值(含存储过程)
/// </summary>
/// <param name="Sql">SQL语句</param>
/// <param name="Prams">参数数组</param>
/// <param name="Type"命令类型</param>
/// <returns>执行结果</returns>
public static int ExecuteScalar(string Sql, SqlParameter[] Prams, CommandType Type)
{
int back = 0;
using (SqlConnection con = new SqlConnection(ConStr))
{
try
{
SqlCommand Command = new SqlCommand(Sql, con);
Command.CommandType = Type;
if (Prams != null)
Command.Parameters.AddRange(Prams);
con.Open();
back = (int)Command.ExecuteScalar();
con.Close();
}
catch (Exception e)
{
string ex = e.Message;
}
}
return back;
}
#endregion
#region 查询并返回一个DataSet
/// <summary>
/// 查询并返回一个DataSet(只有SQL语句,参数、命令类型则默认为null,CommandType.Text)
/// </summary>
/// <param name="Sql">SQL语句</param>
/// <returns>执行结果</returns>
public static DataSet ExecuteAdapter(string Sql)
{
return ExecuteAdapter(Sql, null, CommandType.Text);
}
/// <summary>
/// 查询并返回一个DataSet(命令类型则默认CommandType.Text)
/// </summary>
/// <param name="Sql">SQL语句</param>
/// <param name="Prams">参数数组</param>
/// <returns>执行结果</returns>
public static DataSet ExecuteAdapter(string Sql, SqlParameter[] Prams)
{
return ExecuteAdapter(Sql, Prams, CommandType.Text);
}
/// <summary>
/// 查询并返回一个DataSet(含存储过程)
/// </summary>
/// <param name="Sql">SQL语句</param>
/// <param name="Prams">参数数组</param>
/// <param name="Type">命令类型</param>
/// <returns>执行结果</returns>
public static DataSet ExecuteAdapter(string Sql, SqlParameter[] Prams, CommandType Type)
{
DataSet dataset = new DataSet();
using (SqlConnection con = new SqlConnection(ConStr))
{
try
{
SqlCommand Command = new SqlCommand(Sql, con);
Command.CommandType = Type;
if (Prams != null)
Command.Parameters.AddRange(Prams);
SqlDataAdapter dataadapter = new SqlDataAdapter(Command);
con.Open();
dataadapter.Fill(dataset);
con.Close();
}
catch (Exception e)
{
string ex = e.Message;
}
}
return dataset;
}
#endregion
#region 查询并返回一个SqlDataReader
/// <summary>
/// 查询并返回一个SqlDataReader(只有SQL语句,参数、命令类型则默认为null,CommandType.Text)
/// </summary>
/// <param name="Sql">SQL语句</param>
/// <returns>执行结果</returns>
public static SqlDataReader ExecuteReader(string Sql)
{
return ExecuteReader(Sql, null, CommandType.Text);
}
/// <summary>
/// 查询并返回一个SqlDataReader(命令类型则默认CommandType.Text)
/// </summary>
/// <param name="Sql">SQL语句</param>
/// <param name="Prams">参数数组</param>
/// <returns>执行结果</returns>
public static SqlDataReader ExecuteReader(string Sql, SqlParameter[] Prams)
{
return ExecuteReader(Sql, Prams, CommandType.Text);
}
/// <summary>
/// 查询并返回一个SqlDataReader(含存储过程)
/// </summary>
/// <param name="Sql">SQL语句</param>
/// <param name="Prams">参数数组</param>
/// <param name="Type">命令类型</param>
/// <returns>执行结果</returns>
public static SqlDataReader ExecuteReader(string Sql, SqlParameter[] Prams, CommandType Type)
{
SqlConnection con = new SqlConnection(ConStr);
try
{
con.Open();
SqlCommand com = new SqlCommand(Sql, con);
com.CommandType = Type;
if (Prams != null)
com.Parameters.AddRange(Prams);
SqlDataReader reader = com.ExecuteReader(CommandBehavior.CloseConnection);
com.Parameters.Clear();
return reader;
}
catch (Exception e)
{
string ex = e.Message;
con.Close();
return null;
}
}
#endregion
对象全部变成访问access 就行了 --------------------编程问答-------------------- 数据库操作类sqlhelper
微软企业库
http://topic.csdn.net/u/20101202/14/ddb8f61b-43b2-47d0-b29a-1114f9d86733.html --------------------编程问答-------------------- 麻烦不要用复制粘贴..因为我只要一个功能的,就是带参数更新。会写的话,应该一两分钟就能搞掂的。麻烦,有没有人能帮我写写..谢谢 --------------------编程问答-------------------- 这是我根据微软的sqlhelper简写的,如果还不符合你的意愿,就另请高明,请问你看懂我的代码没有
补充:.NET技术 , ASP.NET