新手请教:数据库连接必须每一个FORM都有一个吗?

菩提998866 2008-09-04 06:33:31
刚学习C#,不明白的问题有不少,在这里想向各位高手请教一下:在一个系统中,如果要用到多个窗体,必须在每个窗体中都单独地建立连接数据库吗,有办法公用一个连接,在各个窗体中都可以用吗?
...全文
185 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
plby 2011-11-18
  • 打赏
  • 举报
回复
回复看看

GhostAdai 2008-09-05
  • 打赏
  • 举报
回复
路过接分。
sxmonsy 2008-09-05
  • 打赏
  • 举报
回复
写一个DBConnection.cs的类.把连接写在里面.别的地方用的话就来这调.
例如:public static ConnectionStr="...连接字符串";
调用: SqlConnection myConnection = new SqlConnection(DBConnection.ConnectionStr);
随烟而逝 2008-09-05
  • 打赏
  • 举报
回复
我也在学习中……

我觉得有些实例的书本基本上有说
你可以看看
zt_100094 2008-09-05
  • 打赏
  • 举报
回复
单写一个连接类就可以了,要用的时就调就是了
beijingbeerman 2008-09-05
  • 打赏
  • 举报
回复
可以单独写一个数据库连接的类,每个form需要链接数据库时可以定义一个实例就可以了。
letisgoto 2008-09-04
  • 打赏
  • 举报
回复
单独建个类处理连接,无论几个窗体,同调这个连接方法。。。。。。。。。。
fengyecsdn 2008-09-04
  • 打赏
  • 举报
回复
如果是WINFORM环境

你完全可以使整个程序只使用一个后台数据库连接。无论是长连或是短连都可以。
作一个公共类,接受请求返回结果。其实没什么复杂的。

但是不建议吧数据库访问“直接”作成静态类。应该是有初始化有释放(实现IDISPOSE,实现DISPOSE方法)而且最好能可靠的释放,是个好风格。 然后在外边再作一层类,可以是静态的类或者方法。这样会有良好的使用习惯。

要是WEB的化,使用一个数据库连接就比较困难而且有危险了。还是各司其职的好,其他理论同上。
孤剑 2008-09-04
  • 打赏
  • 举报
回复
系统可以建立 数据连接池,
建议系统的学习,不要只停留在理论的基础上。
qq283617300 2008-09-04
  • 打赏
  • 举报
回复
一个就可以了。看来真的是初学啊。你还是多找写初学者书看看。加油支持爱学习的人
fuda_1985 2008-09-04
  • 打赏
  • 举报
回复
看看sqlhelp比较好。7-
Magicwords 2008-09-04
  • 打赏
  • 举报
回复
建议系统的学习一下
菩提998866 2008-09-04
  • 打赏
  • 举报
回复
感谢8楼的朋友!谢谢,我要好好学习研究一下!
菩提998866 2008-09-04
  • 打赏
  • 举报
回复
朋友们:把类写成静态的的意思是什么呢?引用时可以用吗?
luluyy 2008-09-04
  • 打赏
  • 举报
回复
把这个写到类里` 变量你要自己定义在用的时候往里面传就行了~~ 不能每个窗体中都写`这样程序的灵活性太差` 如果我不在你的机器上运行了我拿到别的机器上去就不行了`因为连接字符串是不一样的` 再有1`如果程序要改动的话要是写到程序窗体下也会很累~ 写到累里只需要改动类就行了`程序的结构也清晰
luluyy 2008-09-04
  • 打赏
  • 举报
回复

private sealed class sqlheaper()
{
#region ExecuteNonQuery



public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of SqlParameters
return ExecuteNonQuery(connectionString, commandType, commandText, (SqlParameter[])null);
}

public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );

// Create & open a SqlConnection, and dispose of it after we are done
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

// Call the overload that takes a connection in place of the connection string
return ExecuteNonQuery(connection, commandType, commandText, commandParameters);
}
}
public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );
if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( "spName" );

// If we receive parameter values, we need to figure out where they go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
// Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

// Assign the provided values to these parameters based on parameter order
AssignParameterValues(commandParameters, parameterValues);

// Call the overload that takes an array of SqlParameters
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
// Otherwise we can just call the SP without params
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
}
}

public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of SqlParameters
return ExecuteNonQuery(connection, commandType, commandText, (SqlParameter[])null);
}
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if( connection == null ) throw new ArgumentNullException( "connection" );

// Create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection );

// Finally, execute the command
int retval = cmd.ExecuteNonQuery();

// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
if( mustCloseConnection )
connection.Close();
return retval;
}
public static int ExecuteNonQuery(SqlConnection connection, string spName, params object[] parameterValues)
{
if( connection == null ) throw new ArgumentNullException( "connection" );
if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( "spName" );

// If we receive parameter values, we need to figure out where they go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
// Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);

// Assign the provided values to these parameters based on parameter order
AssignParameterValues(commandParameters, parameterValues);

// Call the overload that takes an array of SqlParameters
return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
// Otherwise we can just call the SP without params
return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
}
}

public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of SqlParameters
return ExecuteNonQuery(transaction, commandType, commandText, (SqlParameter[])null);
}

public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if( transaction == null ) throw new ArgumentNullException( "transaction" );
if( transaction != null && transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );

// Create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );

// Finally, execute the command
int retval = cmd.ExecuteNonQuery();

// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
return retval;
}

public static int ExecuteNonQuery(SqlTransaction transaction, string spName, params object[] parameterValues)
{
if( transaction == null ) throw new ArgumentNullException( "transaction" );
if( transaction != null && transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );
if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( "spName" );

// If we receive parameter values, we need to figure out where they go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
// Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

// Assign the provided values to these parameters based on parameter order
AssignParameterValues(commandParameters, parameterValues);

// Call the overload that takes an array of SqlParameters
return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
// Otherwise we can just call the SP without params
return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
}
}

#endregion ExecuteNonQuery
}

wjp_116 2008-09-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 chenyijiu 的回复:]
引用 3 楼 zihua 的回复:
将连接放在一个类库中,用时是新生成实例吧,是不是相当于两个连接?

你可以把类写成静态的
[/Quote]
同意
mywisest 2008-09-04
  • 打赏
  • 举报
回复
如果连接一个数据库,那就用一个好了.
如果连接多个数据库,那就得用多个.
chenyijiu 2008-09-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zihua 的回复:]
将连接放在一个类库中,用时是新生成实例吧,是不是相当于两个连接?
[/Quote]
你可以把类写成静态的
xiaoyue520 2008-09-04
  • 打赏
  • 举报
回复
接分中.........


一个就可以搞定
加载更多回复(3)

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧