日期:2014-05-17  浏览次数:20431 次

怎样在.net中把连接到access数据库改变成连接到SQL数据库??
<add name="SqlConnStr" connectionString="data source =localhost;uid =sq_abzwsj;pwd=sq_wsj369;database=wsj;pooling=true;"

public class ClsSqlconn
{
  private static string SqlConnStr = ConfigurationManager.ConnectionStrings["SqlConnStr"].ConnectionString;

  public ClsSqlconn()
  {
  }

  //打开数据库链接
  public static SqlConnection Open_SqlConn()
  {
  try
  {
  SqlConnection SqlConn = new SqlConnection(SqlConnStr);
  SqlConn.Open();
  return SqlConn;
  }
  catch (Exception Ex)
  {
  throw Ex;
  }
  }

  // 生成Command对象  
  public static SqlCommand Create_SqlCmd(string SqlStr, SqlConnection SqlConn)
  {
  SqlCommand SqlCmd = new SqlCommand(SqlStr, SqlConn);
  return SqlCmd;
  }

  //关闭数据库链接
  public static void Close_SqlConn(SqlConnection SqlConn)
  {
  if (SqlConn != null)
  {
  SqlConn.Close();
  SqlConn.Dispose();
  }
  GC.Collect();
  }

  //运行Sql语句
  public static void Run_SQL(string SQL)
  {
  SqlConnection SqlConn = Open_SqlConn();
  SqlCommand SqlCmd = Create_SqlCmd(SQL, SqlConn);
  try
  {
  //int result_count = 
  SqlCmd.ExecuteNonQuery();
  Close_SqlConn(SqlConn);
  //return result_count;
  }
  catch (Exception Ex)
  {
  throw Ex;
  }
  }


  //运行Sql语句,返回受影响的记录条数
  public static int Run_SQL_Row(string SQL)
  {
  SqlConnection SqlConn = Open_SqlConn();
  SqlCommand SqlCmd = Create_SqlCmd(SQL, SqlConn);
  try
  {
  int result_count = SqlCmd.ExecuteNonQuery();
  Close_SqlConn(SqlConn);
  return result_count;
  }
  catch (Exception Ex)
  {
  throw Ex;
  }
  }

  //返回Sql语句执行结果的第一行第一列
  public static string Get_Row1_Col1_Value(string Sql)
  {
  SqlConnection SqlConn = Open_SqlConn();
  string result;
  result = "";

  SqlDataReader Dr;
  try
  {
  Dr = Create_SqlCmd(Sql, SqlConn).ExecuteReader();
  if (Dr.Read())
  {
  result = Dr[0].ToString();
  }
  Dr.Close();
  }
  catch
  {
  throw new Exception(Sql);
  }
  Close_SqlConn(SqlConn);
  return result;
  }

  // 运行Sql语句返回 SqlDataReader对象
  public static SqlDataReader Get_Reader(string SQL)
  {
  SqlConnection SqlConn = Open_SqlConn();
  SqlCommand SqlCmd = Create_SqlCmd(SQL, SqlConn);
  SqlDataReader Dr;
  try
  {
  Dr = SqlCmd.ExecuteReader(CommandBehavior.Default);
  }
&nb