--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------
using System;
using System.Collections.Generic;using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace MyDataBase
public class MyDataBaseService
{
/// <summary>
/// 获取数据库连接
/// </summary>
/// <returns></returns>
private static SqlConnection GetConn()
{
SqlConnection conn = new SqlConnection();
string connStr = "server=SQL Server连接计算机名称;uid=用户实例账号;pwd=密码;database=数据库名称";
conn.ConnectionString = connStr;
conn.Open();
return conn;
}
/// <summary>
/// 执行修改sql语句,如insert\update\delete
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int ExecuteModi(string sql)
{
using (SqlConnection conn = GetConn())
{
SqlCommand cmd = new SqlCommand();//创建执行命令的对象
cmd.Connection = conn;//指定数据库连接
cmd.CommandText = sql;//指定要执行的sql语句
try
{return cmd.ExecuteNonQuery();//执行, 并返回受影响的行数
}
catch
{
return -2;
}
}
}
/// <summary>
/// 执行查询的sql语句,如select
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataTable OpenQuery(string sql)
{
using (SqlConnection conn = GetConn())
{
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds.Tables[0];
}
}
}
}
--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------