发布网友 发布时间:2022-04-08 04:07
共2个回答
懂视网 时间:2022-04-08 08:29
System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.Data.SqlClient; using System.Data; namespace MySqlHelper { public class SQLHelper { private static readonly string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; /// <summary> /// 连接数据库 /// </summary> /// <returns></returns> public static SqlConnection CreateConnection() { SqlConnection conn = new SqlConnection(connstr); conn.Open(); return conn; } /// <summary> /// 返回受影响行数 /// </summary> /// <param name="conn"></param> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static int ExecuteNonQuery(SqlConnection conn, string sql, params SqlParameter[] parameters) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); return cmd.ExecuteNonQuery(); } } /// <summary> /// 按现有连接,返回受影响行数 /// </summary> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters) { using (SqlConnection conn = CreateConnection()) { return ExecuteNonQuery(conn,sql,parameters); } } /// <summary> /// 返回第一行第一条数据 /// </summary> /// <param name="conn"></param> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static object ExecuteScalar(SqlConnection conn, string sql, params SqlParameter[] parameters) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); return cmd.ExecuteScalar(); } } /// <summary> /// 按现有连接,返回第一行第一条数据 /// </summary> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static object ExecuteScalar(string sql, params SqlParameter[] parameters) { using (SqlConnection conn = CreateConnection()) { return ExecuteScalar(conn,sql,parameters); } } /// <summary> /// 返回多条数据 /// </summary> /// <param name="conn"></param> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static DataTable ExecuteReader(SqlConnection conn, string sql, params SqlParameter[] parameters) { DataTable table = new DataTable(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); using (SqlDataReader reader = cmd.ExecuteReader()) { table.Load(reader); } } return table; } /// <summary> /// 按现有连接,返回多条数据 /// </summary> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static DataTable ExecuteReader(string sql, params SqlParameter[] parameters) { using (SqlConnection conn = CreateConnection()) { return ExecuteReader(conn,sql,parameters); } } } }
Web.config配置文件:
<configuration> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <connectionStrings> <add name="connstr" connectionString="server=PC-20140729GKIU;user id=sa;password=update123;database=sy"/> </connectionStrings> </configuration>
MySqlHelper
标签:
热心网友 时间:2022-04-08 05:37
public ResultSet SQL_ExecuteQuery(String sql,Object [] p){
Connection con=openConnection();//打开数据库
PreparedStatement pst=null;//定义一个 执行对象
try {
pst=con.prepareStatement(sql);//初始化执行对象
//循环设置参数
if(p!=null && p.length>0){
for (int i = 0; i < p.length; i++) {
pst.setObject((i+1), p[i]);//设置参数
}
}
//最后执行命令 并返回结果
return pst.executeQuery();//执行命令
} catch (SQLException e) {
e.printStackTrace();
}finally{
try{pst.close();}catch(SQLException ex){}
try{con.close();}catch(SQLException ex){}
}
return null;
}
百度搞得乱乱的,,,,,
追答//最后执行命令 并返回结果
return pst.executeQuery();//执行命令
换成,返回Vector>