| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 520 人关注过本帖
标题:static 有问题吗?
取消只看楼主 加入收藏
小乌龟
Rank: 2
等 级:新手上路
威 望:4
帖 子:1291
专家分:0
注 册:2007-9-28
结帖率:93.15%
收藏
 问题点数:0 回复次数:1 
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
快速回复:static 有问题吗?
数据加载中...
 
   



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

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