public SqlConnection GetCon()
{
SqlConnection.ClearAllPools();
Con = new SqlConnection(ConnectionString);
Con.Open();
return Con;
for (int i = 0; i < 5000 ; i++)
{
string P_Str_cmdtxt = "Select count(*) from MokuaiGFJStock"; SqlCommand cmd = new SqlCommand(P_Str_cmdtxt,Sql.GetCon());
count=(int)cmd.ExecuteScalar();
cmd.Dispose();
Console.WriteLine(i.ToString() + " ");
} Console.Write( "结束 ");为何运行2000多次的时候,提示数据库连接错误。
string ConnectionString = "Max Pool Size=10; Data Source=10.10.16.17;Initial Catalog=PlanInventoryBook_new;uid=sa;pwd=xujunqq1";
错误内容:“成功与服务器建立连接,但是在登录前的握手期间发生错误。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 0 - 管道的另一端上无任何进程。)”
{
SqlConnection.ClearAllPools();
Con = new SqlConnection(ConnectionString);
Con.Open();
return Con;
for (int i = 0; i < 5000 ; i++)
{
string P_Str_cmdtxt = "Select count(*) from MokuaiGFJStock"; SqlCommand cmd = new SqlCommand(P_Str_cmdtxt,Sql.GetCon());
count=(int)cmd.ExecuteScalar();
cmd.Dispose();
Console.WriteLine(i.ToString() + " ");
} Console.Write( "结束 ");为何运行2000多次的时候,提示数据库连接错误。
string ConnectionString = "Max Pool Size=10; Data Source=10.10.16.17;Initial Catalog=PlanInventoryBook_new;uid=sa;pwd=xujunqq1";
错误内容:“成功与服务器建立连接,但是在登录前的握手期间发生错误。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 0 - 管道的另一端上无任何进程。)”
解决方案 »
- double 数据结果疑问
- 如何实现此类功能-》区域填充颜色
- 【求助,急】在timer内值的计数相关
- web页面导出word文档,解决马上给分
- C# 绑定数据库控件后的相应问题
- managed directx 中如何实现音频的压缩和RTP/UDP网络传送
- Vsual c#.Net编译完的程序,在没有装visual studio的电脑上运行,要装个怎样的程序?
- 求助!由于CF下没有RichTextBox不能进行格式化输出text,想继承TextBox重写,可是不知道override哪个方法
- c# winform如何三层开发
- .Net Web服务与MQMQ的一点问题
- 请教一下,ArrayList是不是不能像数组那样初始化的时候赋值?
- C#生成的PDF怎么去掉段首自动生成的序号
如:using(SqlConnection conn = new Sqlconnection())
{
//
conn.Open();
//下面再写SqlCommand以及执行语句等
}
SqlConnection conn = GetCon();
for (int i = 0; i < 5000 ; i++)
{
string P_Str_cmdtxt = "Select count(*) from MokuaiGFJStock"; SqlCommand cmd = new SqlCommand(P_Str_cmdtxt,conn);
count=(int)cmd.ExecuteScalar();
cmd.Dispose();
Console.WriteLine(i.ToString() + " ");
}
conn.Close();
Console.Write( "结束 ");
每次连接前 判断数据库 连接是否开启,
如果开启就没必要再创建新的连接了
{
SqlDataAdapter Da;
DataSet Ds;
SqlCommand Cmd;
string ConnectionString = "Max Pool Size=10; Data Source=10.10.16.17;Initial Catalog=PlanInventoryBook_new;uid=sa;pwd=xujunqq1";
SqlConnection Con;
/// <summary>
/// 构造函数
/// </summary>
public SqlHelp()
{
} /// <summary>
/// 连接数据库
/// </summary>
/// <returns></returns>
public SqlConnection GetCon()
{
SqlConnection.ClearAllPools();
Con = new SqlConnection(ConnectionString);
Con.Close();
Con.Open();
return Con;
}
/// <summary>
/// 执行SQL语句
/// </summary>
/// <param name="cmdtxt">要执行的SQL语句</param>
/// <returns></returns>
public bool GetExecute(string cmdtxt)
{
Cmd = new SqlCommand(cmdtxt, GetCon());
try
{
Cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
return false;
}
finally
{
if (GetCon().State == ConnectionState.Open)
{
GetCon().Close();
GetCon().Dispose();
}
Cmd.Dispose();
Cmd.Connection.Close();
SqlConnection.ClearAllPools();
//
}
}
/// <summary>
/// 返回数据集
/// </summary>
/// <param name="cmdtxt">需要查询的SQL语句</param>
/// <returns></returns>
public DataSet GetDs(string cmdtxt)
{
try
{
Da = new SqlDataAdapter(cmdtxt, GetCon());
Ds = new DataSet();
Da.Fill(Ds);
return Ds;
}
catch (Exception ex)
{
MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
return null;
}
finally
{
// Ds.Dispose();
SqlConnection.ClearAllPools();
}
}
每次都要调用geton(),数据库连接总被打开后出错。怎么重写啊?
GetCon().Close();
GetCon().Dispose();严重错误!你关闭的是新建的连接,而不是原来是用的那个。