cdzy12153
cdzy12153
2017-10-25 06:34

关于SQLDataReader的问题,菜鸟请大神!!

  • 服务器
  • 数据库
  • sql

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Data;
using System.Threading;

namespace ConsoleApplication3
{
public class Program
{

    static System.Timers.Timer timer;
    private static object locko = new object();

    static void Main(string[] args)
    {

        timer = new System.Timers.Timer();
        timer.Interval = 5000;//计时器间隔
        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);//计时器间隔事件
        timer.Start();//开启计时器
        Console.ReadLine();


    }

    static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        //     Comm.ComStr s = new Comm.ComStr();
        lock (timer)
        {
            try
            {

             //   DateTime.AddMinite(10)

                DbHelperSQL.ConnectionString = "Data Source=192.168.0.198;Initial Catalog=abn_LTGS;User ID=sa;Password=;Persist Security Info=True;MultipleActiveResultSets=False;Packet Size=4096;;";//查询第一台服务器数据库的数据
                StringBuilder sb = new StringBuilder();
                sb.Append("time<15:00  and time <16:00 order by time  ");

                using (SqlDataReader sqlDr = timerToSql(sb.ToString()))
                {
                    while (sqlDr.Read())
                    {


                        //DbHelperSQL.ConnectionString = "  Data Source=127.0.0.1;Initial Catalog=test1;User ID=sa;Password=;Persist Security Info=True;MultipleActiveResultSets=False;Packet Size=4096;";//插入第二台服务器连接字符串
                        //bool b = DbHelperSQL.Exists("//判断是否存在存在就插入不存在就不插入");
                        //if (!b)
                        //{
                        //   //执行插入

                        //}





                    }



                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        //    Console.ReadLine();
    }


    /// <summary>
    /// 查询站数据库 DataReader方式
    /// </summary>
    /// <param name="strWhere">SQL语句条件</param>
    /// <param name="sql">SQL连接字符串</param>
    /// <returns></returns>
    public static SqlDataReader timerToSql(string strWhere)
    {
        //   Comm.ComStr.StrSql = sql;//公共方法传SQL字符串
        StringBuilder strSql = new StringBuilder();
        strSql.Append("select *");
        strSql.Append(" FROM [deviantList] ");
        if (strWhere.Trim() != "")
        {
            strSql.Append(" where " + strWhere);
        }
        return DbHelperSQL.ExecuteReader(strSql.ToString());
    }
}

}

///
/// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
///
/// 查询语句
/// SqlDataReader

    public static SqlDataReader ExecuteReader(string strSQL,string connStr )
    {

        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(strSQL, connection);
        try
        {
            connection.Open();
            SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            return myReader;
        }
        catch (System.Data.SqlClient.SqlException e)
        {
            throw e;
        }   

    }

B服务器开启timer一直读取A服务器的数据,向我种写法会不会导致SQLSERVER资源被耗尽,或线程池资源被耗尽?
看网上资料SQLDateReader一定要关闭,不然会导致线程池被耗尽,但我现在写在using模块里应该执行完了就会自动释放吧?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答