using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Security.Principal;
/// <summary>
/// Summary description for Class1
/// </summary>
namespace DataBase
{
public class DBCom
{
//取得一个数据库的连接
public
static SqlConnection GetConnection()
{
SqlConnection sqlconn = new SqlConnection();
try
{
string strCon = "server=192.168.1.88;database=xixi;uid=xiaoxiao;pwd=1234";
sqlconn.ConnectionString = strCon;
return sqlconn;
}
catch (SqlException e)
{
System.Console.WriteLine(e.Message.ToString());
return null;
}
}
//========================================================================
//执行sql语句返回所影响的行数
public int ExecuteSql(string sql)
{
SqlConnection conn = DBCom.GetConnection();
SqlCommand cmd = conn.CreateCommand();
= sql.Trim();
try
{
conn.Open();
int count = cmd.ExecuteNonQuery();
return count;
}
catch (SqlException ex)
{
System.Console.WriteLine(ex.Message.ToString());
return 0;
}
}
//执行存储过程
public
int ExecuteProc(string procName, SqlParameter[] sp)
{
SqlConnection conn = DBCom.GetConnection();
conn.Open();
SqlCommand cmd = conn.CreateCommand();
= procName.Trim();
= CommandType.StoredProcedure;
foreach (SqlParameter s in sp)
{
cmd.Parameters.Add(s);
}
try
{
int i = cmd.ExecuteNonQuery();
conn.Close();
return i;
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message.ToString());
return 0;
}
}
//==============================================================================
//根据sql 返回数据
public
SqlDataReader GetDataReader(string sql)
{
SqlConnection conn = DBCom.GetConnection();
SqlCommand cmd = conn.CreateCommand();
= sql.Trim();
try
{
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(System.);
return dr;
}
catch (SqlException ex)
{
System.Console.WriteLine(ex.Message.ToString());
return null;
}
}
//===========================================================================
public
DataSet GetDataSet(string Sql, string TableName)
{
SqlConnection conn = DBCom.GetConnection();
try
{
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(Sql, conn);
DataSet ds = new DataSet();
adapter.Fill(ds, TableName);
conn.Close();
return ds;
}
catch (SqlException ex)
{
System.Console.WriteLine(ex.Message.ToString());
return null;
}
}
//通过存储过程返回 数据集
public
DataSet GetDataSet1(string procname, SqlParameter[] sp, string tableName)
{
SqlConnection conn = DBCom.GetConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
= CommandType.StoredProcedure;
= procname.Trim();
try
{
conn.Open();
cmd.Connection = conn;
foreach (SqlParameter p in sp)
{
if (p == null)
{
break;
}
else
{
cmd.Parameters.Add(p);
}
}
//cmd.ExecuteNonQuery();
adapter.SelectCommand = cmd;
adapter.Fill(ds, tableName);
conn.Close();
return ds;
}
catch (SqlException ex)
{
System.Console.WriteLine(ex.Message.ToString());
return null;
}
}
public string re(string a)
{
string s = a.Replace("-_COMPART_-", "");
while (s.Length > 0)
{
if (s.LastIndexOf("\r") == s.Length - 1 || s.LastIndexOf("\n") == s.Length - 1)
{
s = s.Remove(s.Length - 1, 1);
}
else
{
break;
}
}
while (s.Length > 0)
{
if (s.IndexOf("\r") == 0 || s.IndexOf("\n") == 0)
{
s = s.Remove(0, 1);
}
else
{
return s;
}
}
return s;
}
public int ExecuteProc1(string procName, SqlParameter[] sp)
{
SqlConnection conn = DBCom.GetConnection();
conn.Open();
SqlCommand cmd = conn.CreateCommand();
= procName.Trim();
= CommandType.StoredProcedure;
foreach (SqlParameter s in sp)
{
try
{
cmd.Parameters.Add(s);
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message.ToString());
return 0;
}
}
try
{
int i = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
conn.Close();
return i;
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message.ToString());
return 0;
}
}
}
}