using MySql.Data.MySqlClient;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
namespace BaseLibDataProcess
{
public class ClsSQLServer
{
private string SqlConnectionString; //数据库连接
///
/// 构造函数
/// 初始化连接数据库参数
///
public ClsSQLServer(string DataName, string User, string PWD)
{
SqlConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=" + DataName + "; User ID=sa;pwd=sa";
}
///
/// 构造函数
/// 初始化连接数据库参数
///
/// 连接对象
public ClsSQLServer(string ConSqlServer)
{
SqlConnectionString = ConSqlServer;
}
///
/// 打开数据库连接
///
/// 连接
public void Open(SqlConnection cn)
{
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
}
///
/// 关闭数据库连接
///
/// 连接
public void Close(SqlConnection cn)
{
if (cn != null)
{
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
cn.Dispose();
}
}
///
/// 查询
///
/// SQL语句
/// 是否存在
public int operateSql(string str_sql)
{
//string sql = "insert into user(username,password,registerdate) values('啊宽','123','" + DateTime.Now + "')";
//string sql = "delete from user where userid='9'";
//string sql = "update user set username='啊哈',password='123' where userid='8'";
int result = 0;
SqlConnection sqlCon = new SqlConnection(SqlConnectionString);
using (SqlCommand cmd = new SqlCommand(str_sql, sqlCon))
{
try
{
// MySqlCommand cmd = new MySqlCommand(str_sql, conn);
result = cmd.ExecuteNonQuery();//3.执行插入、删除、更改语句。执行成功返回受影响的数据的行数,返回1可做true判断。执行失败不返回任何数据,报错,下面代码都不执行
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
sqlCon.Close();
}
}
return result;
}
}
}