12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- 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; //数据库连接
- /// <summary>
- /// 构造函数
- /// 初始化连接数据库参数
- /// </summary>
- public ClsSQLServer(string DataName, string User, string PWD)
- {
- SqlConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=" + DataName + "; User ID=sa;pwd=sa";
- }
- /// <summary>
- /// 构造函数
- /// 初始化连接数据库参数
- /// </summary>
- /// <param name="ConSqlServer">连接对象</param>
- public ClsSQLServer(string ConSqlServer)
- {
- SqlConnectionString = ConSqlServer;
- }
- /// <summary>
- /// 打开数据库连接
- /// </summary>
- /// <param name="cn">连接</param>
- public void Open(SqlConnection cn)
- {
- if (cn.State == ConnectionState.Closed)
- {
- cn.Open();
- }
- }
- /// <summary>
- /// 关闭数据库连接
- /// </summary>
- /// <param name="cn">连接</param>
- public void Close(SqlConnection cn)
- {
- if (cn != null)
- {
- if (cn.State == ConnectionState.Open)
- {
- cn.Close();
- }
- cn.Dispose();
- }
- }
- /// <summary>
- /// 查询
- /// </summary>
- /// <param name="strSql">SQL语句</param>
- /// <returns>是否存在</returns>
- 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;
- }
- }
- }
|