using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace qqLogjin
{
class DBHELPER
{
// 添加 删除 和 修改通用方法
public static int ExcuteNonQuery(string sql,params SqlParameter[] para)
{
int rows = 0;
using (SqlConnection cnn = new SqlConnection("Data Source=.;Initial Catalog=ClassBegin;Integrated Security=True"))
{
cnn.Open();
SqlCommand command = new SqlCommand(sql, cnn);
command.Parameters.AddRange(para);
rows = command.ExecuteNonQuery();
}
return rows;
}
public static Object ExcuteScalar(string sql, params SqlParameter[] para)
{
//连接式的方式
SqlConnection sqlConnection = new SqlConnection("Data Source=.;Initial Catalog=ClassBegin;Integrated Security=True");
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.Parameters.AddRange(para);
Object obj = sqlCommand.ExecuteScalar();
return obj; // 返回影响的行数
}
//public static SqlDataReader ExcuteReader(string sql, params SqlParameter[] para)
//{
// SqlConnection sqlConnection = new SqlConnection("Data Source=.;Initial Catalog=ClassBegin;Integrated Security=True");
// sqlConnection.Open();
// SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
// sqlCommand.Parameters.AddRange(para);
// SqlDataReader reader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
// return reader;
//}
// 断开的方式,不需要释放
public static DataTable ExcuteReader(string sql, params SqlParameter[] para)
{
SqlConnection sqlConnection = new SqlConnection("Data Source=.;Initial Catalog=ClassBegin;Integrated Security=True");
sqlConnection.Open();
SqlCommand command = new SqlCommand(sql, sqlConnection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
return dataTable;
}
}
}
05-05
744
01-18
1828
09-02
1219
09-06
680