DBHelper类怎么写啊
发布网友
发布时间:2022-04-30 12:07
我来回答
共2个回答
热心网友
时间:2022-04-30 13:36
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Load
{
class DBHelper
{
private static SqlCommand cmd = null;
private static SqlDataReader dr = null;
public int RowCount { get; private set; }
SqlConnection sqlCnn = new SqlConnection();
SqlCommand sqlCmd = new SqlCommand();
//数据库连接字符串
private static string connectionString = "Server = 127.0.0.1; Database = DB; Integrated Security =SSPI";
//数据库连接Connection对象
public static SqlConnection connection = new SqlConnection(connectionString);
public DBHelper()
{ }
#region 返回结果集
public static SqlDataReader GetResult(string sql)
{
try
{
cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = connection;
cmd.Connection.Open();
dr = cmd.ExecuteReader();
return dr;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
finally
{
}
}
#endregion
#region 对Select语句,返回int型结果集
public static int GetSqlResult(string sql)
{
try
{
cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = connection;
cmd.Connection.Open();
int a = (int)cmd.ExecuteScalar();
return a;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return -1;
}
finally
{
cmd.Connection.Close();
}
}
#endregion
#region 对Update,Insert和Delete语句,返回该命令所影响的行数
public static int GetDsqlResult(string sql)
{
try
{
cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = connection;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
return 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return -1;
}
finally
{
cmd.Connection.Close();
}
}
#endregion
}
}
热心网友
时间:2022-04-30 14:54
给你个建议, 你可以百度下 找一个适合自己的