using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
//mssdao工具,在项目中引用即可使用
namespace MSSQLDAO
{
public class MSSQLDAO
{
//返回数据库连接
public static SqlConnection getCon (String serverAddress, String userid, String password, String database)
{
SqlConnection con = null;
String conStr;//, conStr1;
//用户名和密码登陆的sqlstring
conStr = "Server=" + serverAddress + ";Database=" + database + ";user id=" + userid + ";password=" + password + ";";
//使用信任登陆的sqlstring
// conStr1 = "Server=127.0.0.1;Database=zw0001;Trusted_Connection=SSPI;";
// System.Windows.Forms.MessageBox.Show("连接字符串:"+conStr);
con = new SqlConnection(conStr);//初始化sqlconnection
//尝试打开链接,验证是否连接字符串正确与否
con.Open();
//关闭连接
con.Close();
return con;
}
//使用信任连接登录。
public static SqlConnection getTrustedCon (String serverAddress, String Database)
{
SqlConnection con = null;
String conStr;//, conStr1;
//用户名和密码登陆的sqlstring
// conStr = "Server=" + serverName + ";Database=" + database + ";user id=" + userid + ";password=" + password + ";";
// 使用信任登陆的sqlstring
conStr = "Server="+ serverAddress + ";Database="+Database+";Trusted_Connection=SSPI;";
// System.Windows.Forms.MessageBox.Show("连接字符串:"+conStr);
con = new SqlConnection(conStr);//初始化sqlconnection
//尝试打开链接,验证是否连接字符串正确与否
con.Open();
//关闭连接
con.Close();
return con;
}
//返回获取到的数据库返回内容
public static DataSet getDataSet(String sqlString, String tableName, SqlConnection con)
{
SqlDataAdapter sqladapter = null;
DataSet set = new DataSet();
if (!con.State.Equals(ConnectionState.Open))
{
con.Open();
}
sqladapter = new SqlDataAdapter(sqlString, con);
sqladapter.Fill(set, tableName);
//关闭连接
con.Close();
return set;
}
//
public static SqlDataAdapter getSQLAdapter(String sqlString, String tableName, SqlConnection con)
{
//SqlDataAdapter sqladapter = null;
//DataSet set = new DataSet();
// if (!con.State.Equals(ConnectionState.Open))
// {
// con.Open();
// }
// sqladapter = new SqlDataAdapter(sqlString, con);
//return sqladapter;
关闭连接
//con.Close();
try
{
SqlConnection objConn = con;
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandTimeout = 600;//unit:seconds
sqlCommand.Connection = objConn;
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.Clear();
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
sqlCommand.CommandText = sqlString;
//if (sqlParameters != null)
//{
// sqlCommand.Parameters.AddRange(sqlParameters.ToArray());
//}
return sqlDataAdapter;
}
}
catch
{
return null;
}
}
//执行命令和存储过程
public static int execSQL(String sqlString, SqlConnection con) {
SqlDataAdapter sqladapter = null;
DataSet set = new DataSet();
SqlCommand sc = new SqlCommand(sqlString, con);
if (!con.State.Equals(ConnectionState.Open))
{
con.Open();
}
int result = sc.ExecuteNonQuery();
//返回影响的行数
con.Close();
return result;
}
}
}
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
//mssdao工具,在项目中引用即可使用
namespace MSSQLDAO
{
public class MSSQLDAO
{
//返回数据库连接
public static SqlConnection getCon (String serverAddress, String userid, String password, String database)
{
SqlConnection con = null;
String conStr;//, conStr1;
//用户名和密码登陆的sqlstring
conStr = "Server=" + serverAddress + ";Database=" + database + ";user id=" + userid + ";password=" + password + ";";
//使用信任登陆的sqlstring
// conStr1 = "Server=127.0.0.1;Database=zw0001;Trusted_Connection=SSPI;";
// System.Windows.Forms.MessageBox.Show("连接字符串:"+conStr);
con = new SqlConnection(conStr);//初始化sqlconnection
//尝试打开链接,验证是否连接字符串正确与否
con.Open();
//关闭连接
con.Close();
return con;
}
//使用信任连接登录。
public static SqlConnection getTrustedCon (String serverAddress, String Database)
{
SqlConnection con = null;
String conStr;//, conStr1;
//用户名和密码登陆的sqlstring
// conStr = "Server=" + serverName + ";Database=" + database + ";user id=" + userid + ";password=" + password + ";";
// 使用信任登陆的sqlstring
conStr = "Server="+ serverAddress + ";Database="+Database+";Trusted_Connection=SSPI;";
// System.Windows.Forms.MessageBox.Show("连接字符串:"+conStr);
con = new SqlConnection(conStr);//初始化sqlconnection
//尝试打开链接,验证是否连接字符串正确与否
con.Open();
//关闭连接
con.Close();
return con;
}
//返回获取到的数据库返回内容
public static DataSet getDataSet(String sqlString, String tableName, SqlConnection con)
{
SqlDataAdapter sqladapter = null;
DataSet set = new DataSet();
if (!con.State.Equals(ConnectionState.Open))
{
con.Open();
}
sqladapter = new SqlDataAdapter(sqlString, con);
sqladapter.Fill(set, tableName);
//关闭连接
con.Close();
return set;
}
//
public static SqlDataAdapter getSQLAdapter(String sqlString, String tableName, SqlConnection con)
{
//SqlDataAdapter sqladapter = null;
//DataSet set = new DataSet();
// if (!con.State.Equals(ConnectionState.Open))
// {
// con.Open();
// }
// sqladapter = new SqlDataAdapter(sqlString, con);
//return sqladapter;
关闭连接
//con.Close();
try
{
SqlConnection objConn = con;
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandTimeout = 600;//unit:seconds
sqlCommand.Connection = objConn;
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.Clear();
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
sqlCommand.CommandText = sqlString;
//if (sqlParameters != null)
//{
// sqlCommand.Parameters.AddRange(sqlParameters.ToArray());
//}
return sqlDataAdapter;
}
}
catch
{
return null;
}
}
//执行命令和存储过程
public static int execSQL(String sqlString, SqlConnection con) {
SqlDataAdapter sqladapter = null;
DataSet set = new DataSet();
SqlCommand sc = new SqlCommand(sqlString, con);
if (!con.State.Equals(ConnectionState.Open))
{
con.Open();
}
int result = sc.ExecuteNonQuery();
//返回影响的行数
con.Close();
return result;
}
}
}