首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# winform 数据库连接解决思路

2012-04-05 
c# winform 数据库连接各位在用c#写winform 数据库应用项目时,数据库连接1、在系统启动的时候创建,一直保持

c# winform 数据库连接
各位在用c#写winform 数据库应用项目时,数据库连接
1、在系统启动的时候创建,一直保持连接直到系统退出
2、随用随创建,用完释放
这两种方式,各有什么优缺点,各位在项目中一般用的哪种方式?

[解决办法]
1、在系统启动的时候创建,一直保持连接直到系统退出
--------------------------------------------
连续批量处理好多数据的时候。比如系统里几百分条图像数据,你要通过winform读出图像进行处理后再写回去。这种时候为每一个图像数据打开/关闭一次连接实在无聊。


通常情况下当然是第二种了。
[解决办法]
mark

[解决办法]
一般是如下用法:
Sqlconnect conn;
以后在用到的时候conn.open();
用过之后,马上conn.close();

[解决办法]
第二种好,比较灵活
[解决办法]
第二种好啊!!!可以节省资源!!!
[解决办法]
我们公司还是用第一种方法。。。。
[解决办法]
必然是第二种
[解决办法]
第一种没必要
你连接数一多,莫名其妙的错误马上就来了

理由:
1.Ado.net的优点就在于断线连接,你搞个一直在线的,那不又把人家给弄回去了
[解决办法]
第二种好
不用实时的连接数据库,可以减少资源利用
[解决办法]
随用随即开,用完就关闭的吧
这样出错的几率小
[解决办法]
我们做的时候是第二种:
其实也不麻烦,
用下using
或者Dispore就释放资源了嘛。
[解决办法]
对电脑也应该有好处
最好是连接完了就关闭
[解决办法]
public class DatabaseServer
{
private SqlConnection conn;
public SqlConnection Connection
{
get
{
try
{
if (conn == null)
{
conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["SY"].ConnectionString;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return conn;
}

}
public int ExecuteSql(string sql)
{
SqlCommand cmmand = null;
try
{
this.Connection.Open();
cmmand = new SqlCommand();
cmmand.Connection = Connection;
cmmand.CommandType = CommandType.Text;
cmmand.CommandText = sql;

return cmmand.ExecuteNonQuery();

}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
cmmand.Connection.Close();
}
}
public DataSet ExecuteSql(string sql, string tabName)
{
try
{
this.Connection.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, Connection);
da.Fill(ds, tabName);
return ds;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
Connection.Close();


}
}
public SqlCommand QueryCommands(string procName, string[] names, string[] values)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = Connection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = procName;
for (int i = 0; i < names.Length; i++)
{
SqlParameter p = new SqlParameter(names[i], values[i]);
cmd.Parameters.Add(p);
}
return cmd;
}
public void ExecuteSql(string procName, string[] names, string[] values)
{
try
{
Connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = Connection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = procName;
for (int i = 0; i < names.Length; i++)
{
SqlParameter p = new SqlParameter(names[i], values[i]);
cmd.Parameters.Add(p);
}
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
Connection.Close();
}
}
public DataSet ExecuteSql(string procName, string tabName, string[] names, string[] values)
{
try
{
Connection.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = QueryCommands(procName, names, values);
da.Fill(ds, tabName);
return ds;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
Connection.Close();
}
}
public void ExecuteSql(string sql, SqlParameter[] parameters)
{
SqlCommand cmd = null;
try
{
Connection.Open();
cmd = new SqlCommand();
cmd.Connection = Connection;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
foreach (SqlParameter p in parameters)
{
cmd.Parameters.Add(p);
}
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
cmd.Connection.Close();
}
}
public SqlDataReader ExecuteReader(string sql)
{
SqlCommand cmd = null;
try
{
Connection.Open();
cmd = new SqlCommand();
cmd.Connection = Connection;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
cmd.Dispose();
cmd.Connection.Close();
}
}
public SqlDataReader ExecuteReader(string procName, IDataParameter[] Parameters)
{
try
{
Connection.Open();
SqlCommand cmd = BuildQueryCommand(Connection, procName, Parameters);


cmd.CommandType = CommandType.StoredProcedure;
return cmd.ExecuteReader();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public SqlCommand BuildQueryCommand(SqlConnection connection, string procName, IDataParameter[] Parameters)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = procName;
foreach (SqlParameter p in Parameters)
{
cmd.Parameters.Add(p);
}
return cmd;
}
[解决办法]

引用楼主 iwalk 的帖子:
各位在用c#写winform 数据库应用项目时,数据库连接
1、在系统启动的时候创建,一直保持连接直到系统退出
2、随用随创建,用完释放
这两种方式,各有什么优缺点,各位在项目中一般用的哪种方式?

[解决办法]
一向用后者, 减少数据库压力...
[解决办法]
具体还是看你的应用场景。
我一半用第二种。
[解决办法]
感觉还是第一种。只连接一次。能保证程序的速度。
[解决办法]
必然是第二种
[解决办法]
一般用后一种好一些

热点排行