标题:static 有问题吗?
只看楼主
小乌龟
Rank: 2
等 级:新手上路
威 望:4
帖 子:1291
专家分:0
注 册:2007-9-28
结帖率:93.15%
 问题点数:0 回复次数:3 
static 有问题吗?

public static SqlConnection GetConnection()
{

SqlConnection sqlconn = new SqlConnection();
try
{
//sqlconn.ConnectionString = ConfigurationSettings.AppSettings["lwConnectionString"];
string strCon = "$$$$$$$$$$$$$$$$$$$$$$$$";
sqlconn.ConnectionString = strCon;
//sqlconn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[1].ConnectionString;
return sqlconn;

}
catch (SqlException e)
{
System.Console.WriteLine(e.Message.ToString());
return null;
}
}

//========================================================================

//执行sql语句返回所影响的行数
public static int ExecuteSql(string sql)
{
SqlConnection conn = DBCom.GetConnection();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql.Trim();
try
{
conn.Open();
int count = cmd.ExecuteNonQuery();
return count;
}
catch (SqlException ex)
{
System.Console.WriteLine(ex.Message.ToString());
return 0;
}
}

//执行存储过程
public static int ExecuteProc(string procName, SqlParameter[] sp)
{
SqlConnection conn = DBCom.GetConnection();
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = procName.Trim();
cmd.CommandType = 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 static SqlDataReader GetDataReader(string sql)
{
SqlConnection conn = DBCom.GetConnection();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql.Trim();
try
{
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
return dr;
}
catch (SqlException ex)
{
System.Console.WriteLine(ex.Message.ToString());
return null;
}
}

//===========================================================================

public static 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 static DataSet GetDataSet(string procname, SqlParameter[] sp, string tableName)
{
SqlConnection conn = DBCom.GetConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = 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;
}
}
我把方法都写成静态的有问题吗???????????????

[此贴子已经被作者于2007-11-15 13:46:28编辑过]

搜索更多相关主题的帖子: static sqlconn strCon string 
2007-11-15 11:13
小乌龟
Rank: 2
等 级:新手上路
威 望:4
帖 子:1291
专家分:0
注 册:2007-9-28
得分:0 

都写成static方法 有问题吗??????????????会不会影响效率?

2007-11-15 11:14
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
得分:0 
占内存

[此贴子已经被作者于2007-11-15 11:32:16编辑过]



飘过~~
2007-11-15 11:31
chinaly
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-9-4
得分:0 
呵呵!!不过全部写成静态不好吧!资源占用很大啊!!

2007-11-15 11:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-186102-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.121675 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved