请高手给帮着看一下

chenguang79 2009-02-10 03:50:34
现在我要写一个控制台程序,每隔二秒钟运行一下下面的sql代码。哪位兄弟能给段代码啊。
declare @insertDatetime datetime
set @insertDatetime = getdate()
insert into Computer2.xlt_cl.dbo.sms_send (memb_login,send_dist,subtype,srv_id,content,fee_type,fee_code,V_ICPCode)

select memb_login,send_dist,subtype,srv_id,content,fee_type,fee_code,V_ICPCode from sms_send where inserted = 1 and insertDatetime <=@insertDatetime

update sms_send set inserted = 0 where insertdatetime <=@insertDatetime

小弟一直做asp.net一下做控制台程序,不知道如何下手。
...全文
134 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
sikongwunai 2009-02-10
  • 打赏
  • 举报
回复
学习中
net_boy 2009-02-10
  • 打赏
  • 举报
回复
直接用sql server 就能实现,很久没开发sql server 都忘了,好像是叫event还是什么的吧,写个存储过程,然后定时执行那个存储过程就行了。
lovehongyun 2009-02-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 chenguang79 的回复:]
这样多条的SQL语句,我不知道如何运行啊。能给段代码吗
[/Quote]

sql直接写就行,当sql执行
给个例子
string sql = @"
DECLARE @i int
SELECT @i = COUNT(*) FROM xxxx WHERE groupId = @gid
IF @i = 0
INSERT INTO xxxxxGroup(groupId) VALUES(@gid)";

SqlCommand cmd = new SqlCommand(sql,conn);
....
...

你的类似
devilli 2009-02-10
  • 打赏
  • 举报
回复
mark
tigerleq 2009-02-10
  • 打赏
  • 举报
回复
else
{
this.button_start.Enabled = false;
this.button_pause.Enabled = false;
this.button_stop.Enabled = false;
this.ToolStripMenuItem_start.Enabled = false;
this.ToolStripMenuItem_pause.Enabled = false;
this.ToolStripMenuItem_stop.Enabled = false;
//bAllServiceStart = false;
}
}
//using (ServiceController sc = new ServiceController( this.m_serverParas.appServerParas.MsgServiceName ))
//{
// if(sc.Status==ServiceControllerStatus.Running)
// {
// this.mnuStartMsgServer.Enabled = false;
// this.mnuStopMsgServer.Enabled = true;
// bAllServiceStop = false;
// }
// else if( sc.Status == ServiceControllerStatus.Stopped )
// {
// this.mnuStartMsgServer.Enabled = true;
// this.mnuStopMsgServer.Enabled = false;
// bAllServiceStart = false;
// }
// else
// {
// this.mnuStartMsgServer.Enabled = false;
// this.mnuStopMsgServer.Enabled = false;
// bAllServiceStart = false;
// }
//}
//using (ServiceController sc = new ServiceController(this.m_serverParas.appServerParas.UpdateServiceName))
//{
// if (sc.Status == ServiceControllerStatus.Running)
// {
// this.ToolStripMenuItem_startUpdate.Enabled = false;
// this.ToolStripMenuItem_stopUpdate.Enabled = true;
// bAllServiceStop = false;
// }
// else if (sc.Status == ServiceControllerStatus.Stopped)
// {
// this.ToolStripMenuItem_startUpdate.Enabled = true;
// this.ToolStripMenuItem_stopUpdate.Enabled = false;
// bAllServiceStart = false;
// }
// else
// {
// this.ToolStripMenuItem_startUpdate.Enabled = false;
// this.ToolStripMenuItem_stopUpdate.Enabled = false;
// bAllServiceStart = false;
// }
//}
//if (bAllServiceStart)
//{
// this.mnuitmStartAllService.Enabled = false;
// this.mnuitmStopAllService.Enabled = true;
//}
//if (bAllServiceStop)
//{
// this.mnuitmStopAllService.Enabled = false;
// this.mnuitmStartAllService.Enabled = true;
//}
//if (!bAllServiceStart && !bAllServiceStop)
//{
// this.mnuitmStartAllService.Enabled = true;
// this.mnuitmStopAllService.Enabled = true;
//}
}
catch (Exception ex)
{
logOperate = new LogOperate();
logOperate.OperateExp(ex, "在根据服务的当前状态更新控件状态时出错", "");
}
}

private void tmrAutoStart_Tick(object sender, EventArgs e)
{
LogErr logErr = null;
LogOperate logOperate = null;
try
{
Start();
this.tmrAutoStart.Enabled = false;
}
catch (AppErrException)
{
}
catch (Exception ex)
{
logErr = new LogErr(ex, "在自动启动服务时出错");
logOperate = new LogOperate();
logOperate.Write(logErr);
}
}

public void Start()
{
LogOperate logOperate = null;
try
{
using (ServiceController sc = new ServiceController(this.m_serverParas.appServerParas.AppServiceName))
{
if (sc.Status == ServiceControllerStatus.Running)
{
return;
}
if (sc.Status == ServiceControllerStatus.Paused)
{
sc.Continue();
}
else
{
sc.Start();
}
}
this.RefreshService();
}
catch (Exception ex)
{
logOperate = new LogOperate();
logOperate.OperateExp(ex, "在启动应用服务器的服务时出错", "");
}
}

}
}
winform实现

[/code]
tigerleq 2009-02-10
  • 打赏
  • 举报
回复
private void button_start_Click(object sender, EventArgs e)
{
LogErr logErr = null;
LogOperate logOperate = null;
try
{
Start();
}
catch (AppErrException)
{
MessageBox.Show("在启动应用服务器服务时出错");
}
catch (Exception ex)
{
logErr = new LogErr(ex, "在启动应用服务器服务时出错");
logOperate = new LogOperate();
logOperate.Write(logErr);
MessageBox.Show("在启动应用服务器服务时出错");
}
}

private void button_pause_Click(object sender, EventArgs e)
{
LogErr logErr = null;
LogOperate logOperate = null;
try
{
using (ServiceController sc = new ServiceController(this.m_serverParas.appServerParas.AppServiceName))
{
if (sc.Status == ServiceControllerStatus.Running)
{

if (sc.CanPauseAndContinue)
{
sc.Pause();
}
else
{
MessageBox.Show("当前服务不能暂停!", "提示");
return;
}
}
}
this.RefreshService();
}
catch (AppErrException)
{
MessageBox.Show("在暂停应用服务器服务时出错");
}
catch (Exception ex)
{
logErr = new LogErr(ex, "在暂停应用服务器服务时出错");
logOperate = new LogOperate();
logOperate.Write(logErr);
MessageBox.Show("在暂停应用服务器服务时出错");
}
}

private void button_stop_Click(object sender, EventArgs e)
{
LogErr logErr = null;
LogOperate logOperate = null;
try
{
using (ServiceController sc = new ServiceController(this.m_serverParas.appServerParas.AppServiceName))
{
if (sc.Status != ServiceControllerStatus.Stopped)
{
if (sc.CanShutdown)
{
sc.Stop();
}
else
{
MessageBox.Show("当前服务不能停止!","提示");
return;
}
}
}
this.RefreshService();
}
catch (AppErrException)
{
MessageBox.Show("在停止应用服务器服务时出错");
}
catch (Exception ex)
{
logErr = new LogErr(ex, "在停止应用服务器服务时出错");
logOperate = new LogOperate();
logOperate.Write(logErr);
MessageBox.Show("在停止应用服务器服务时出错");
}
}

private void button_refresh_Click(object sender, EventArgs e)
{
LogErr logErr = null;
LogOperate logOperate = null;
try
{
this.RefreshService();
}
catch (AppErrException)
{
MessageBox.Show("在刷新应用服务器状态时出错");
}
catch (Exception ex)
{
logErr = new LogErr(ex, "在刷新应用服务器状态时出错");
logOperate = new LogOperate();
logOperate.Write(logErr);
MessageBox.Show("在刷新应用服务器状态时出错");
}
}

private void tmrRefresh_Tick(object sender, EventArgs e)
{
LogErr logErr = null;
LogOperate logOperate = null;
try
{
this.RefreshService();
}
catch (AppErrException)
{
}
catch (Exception ex)
{
logErr = new LogErr(ex, "在刷新应用服务器状态时出错");
logOperate = new LogOperate();
logOperate.Write(logErr);
}
}



private void RefreshService()
{
LogOperate logOperate = null;
//bool bAllServiceStart = true;
//bool bAllServiceStop = true;
try
{
GC.Collect();
GC.Collect();
using (ServiceController sc = new ServiceController(this.m_serverParas.appServerParas.AppServiceName))
{
ServiceControllerStatus aa = sc.Status;
if (sc.Status == ServiceControllerStatus.Running)
{
this.button_start.Enabled = false;
this.button_pause.Enabled = true;
this.button_stop.Enabled = true;

this.label_start.Enabled = false;
this.label_pause.Enabled = true;
this.label_stop.Enabled = true;

this.ToolStripMenuItem_start.Enabled = false;
this.ToolStripMenuItem_pause.Enabled = true;
this.ToolStripMenuItem_stop.Enabled = true;
this.notifyIcon1.Icon = Icon.ExtractAssociatedIcon(@"ICO\network.ico");
//bAllServiceStop = false;
}
else if (sc.Status == ServiceControllerStatus.Stopped)
{
this.button_start.Enabled = true;
this.button_pause.Enabled = false;
this.button_stop.Enabled = false;

this.label_start.Enabled = true;
this.label_pause.Enabled = false;
this.label_stop.Enabled = false;

this.ToolStripMenuItem_start.Enabled = true;
this.ToolStripMenuItem_pause.Enabled = false;
this.ToolStripMenuItem_stop.Enabled = false;
this.notifyIcon1.Icon = Icon.ExtractAssociatedIcon(@"ICO\stop.ico");
this.tmrAutoStart.Enabled = true;
// bAllServiceStart = false;
}
else if (sc.Status == ServiceControllerStatus.Paused)
{
this.button_start.Enabled = true;
this.button_pause.Enabled = false;
this.button_stop.Enabled = true;

this.label_start.Enabled = true;
this.label_pause.Enabled = false;
this.label_stop.Enabled = true;

this.ToolStripMenuItem_start.Enabled = true;
this.ToolStripMenuItem_pause.Enabled = false;
this.ToolStripMenuItem_stop.Enabled = true;
this.notifyIcon1.Icon = Icon.ExtractAssociatedIcon(@"ICO\pause.ico");
this.tmrAutoStart.Enabled = true;
}
tigerleq 2009-02-10
  • 打赏
  • 举报
回复
在写个服务
控制程序

using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Runtime.Serialization;
using System.Diagnostics;
using System.Xml;
using TigerLeqClassLibrary.ServerConfig;
using TigerLeqClassLibrary.Log;
using TigerLeqClassLibrary.UserException;

namespace Window_ContralService
{
public partial class Form_Service_Main : Form
{

private string m_sServerConfigFile = Application.StartupPath + "\\Config\\ServerConfig.xml";
private ServerParas m_serverParas = null;
public Form_Service_Main()
{
InitializeComponent();

this.m_serverParas = new ServerParas(this.m_sServerConfigFile);
double dInterval = this.m_serverParas.appServerParas.AppConfigRefreshInterval * 1000;
this.tmrRefresh.Interval = int.Parse(dInterval.ToString());
this.tmrRefresh.Enabled = true;
}

private void Form_Service_Main_FormClosing(object sender, FormClosingEventArgs e)
{
this.Visible = false;
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}

private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
Show_Form();
}

private void Show_Form()
{

if (this.WindowState == FormWindowState.Minimized || this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
}
this.Activate();
this.Visible = true;
}

private void toolStripMenuItem_configset_Click(object sender, EventArgs e)
{
Show_Form();
}

private void toolStripMenuItem_exit_Click(object sender, EventArgs e)
{
this.Dispose();
}

tigerleq 2009-02-10
  • 打赏
  • 举报
回复
[code=C#]
写个
服务
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Runtime.Remoting;
using System.Windows.Forms;
using System.Xml;
using TigerLeqClassLibrary.Log;
using TigerLeqClassLibrary.UserException;
using TigerLeqClassLibrary.ServerConfig;

namespace WindowsService
{
public partial class MainService : ServiceBase
{
private string m_sServerConfigFile = Application.StartupPath + "\\Config\\ServerConfig.xml";
private ServerParas m_serverParas = null;

public MainService()
{
InitializeComponent();
try
{
this.m_serverParas = new ServerParas(this.m_sServerConfigFile);
}
catch (System.Exception ex)
{
LogOperate logOperate = new LogOperate();
logOperate.OperateExp(ex, "应用服务构造函数中读取配置信息时出错", "");
}
}

public void Start(string[] args)
{
OnStart(args);
}


protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
LogOperate logOperate = null;
LogInfo logInfo = new LogInfo("开始启动MainService服务", "");
logOperate = new LogOperate();
logOperate.Write(logInfo);
try
{
RemotingConfiguration.Configure(Application.StartupPath + "\\Config\\" + this.m_serverParas.appServerParas.RemoteConfigFile,false);
logInfo = new LogInfo("成功启动MainService服务", "");
logOperate = new LogOperate();
logOperate.Write(logInfo);
}
catch (AppErrException)
{
return;
}
catch (Exception ex)
{
LogErr logErr = new LogErr(ex, "在启动MainService服务时出错");
logOperate = new LogOperate();
logOperate.Write(logErr);
return;
}
}

protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
LogOperate logOperate = null;
LogInfo logInfo = new LogInfo("停止MainService服务", "");
logOperate = new LogOperate();
logOperate.Write(logInfo);
}

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
LogOperate logOperate = null;
LogInfo logInfo = new LogInfo("登陆信息========================", "");
logOperate = new LogOperate();
logOperate.Write(logInfo);
GC.Collect();
}


protected override void OnPause()
{
LogOperate logOperate = null;
LogInfo logInfo = new LogInfo("暂停MainService服务", "");
logOperate = new LogOperate();
logOperate.Write(logInfo);
}
protected override void OnContinue()
{
LogOperate logOperate = null;
LogInfo logInfo = new LogInfo("继续MainService服务", "");
logOperate = new LogOperate();
logOperate.Write(logInfo);
}


/// <summary>
///
/// </summary>
/// <param name="powerStatus"></param>
/// <returns></returns>
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
LogOperate logOperate = null;
LogInfo logInfo = new LogInfo("计算机的电源状态改变:" + powerStatus.ToString(), "");
logOperate = new LogOperate();
logOperate.Write(logInfo);
return true;
}
protected override void OnShutdown()
{
LogOperate logOperate = null;
LogInfo logInfo = new LogInfo("操作系统关闭", "");
logOperate = new LogOperate();
logOperate.Write(logInfo);
}
}
}

whitechololate 2009-02-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lovehongyun 的回复:]
C# code
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication3
{
class TimerExampleState
{
public int counter = 0;
public Timer tmr;
}

class Program
{
static void Main(string[] args)
{
TimerExampleState s = new TimerExampleState();

[/Quote]

学习了,帮顶
lbxlnet 2009-02-10
  • 打赏
  • 举报
回复
来学习下,,,
chenguang79 2009-02-10
  • 打赏
  • 举报
回复
这样多条的SQL语句,我不知道如何运行啊。能给段代码吗
lovehongyun 2009-02-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chenguang79 的回复:]
现在不是开发web程序,是一个别的程序要用到这个。我要写一个控制台程序。每二秒来完成这些语句。有没有兄弟能告诉我一下啊
[/Quote]

其实你可以写个windows services来代替控制台程序,非要用控制台程序就看3楼
lovehongyun 2009-02-10
  • 打赏
  • 举报
回复
用上面我发的代码即可.时间自己调整

我注释掉的代码是用来停止的,你要写在哪,什么时候停止,就自己决定吧
s.tmr.Dispose();
s.tmr = null;
chenguang79 2009-02-10
  • 打赏
  • 举报
回复
现在不是开发web程序,是一个别的程序要用到这个。我要写一个控制台程序。每二秒来完成这些语句。有没有兄弟能告诉我一下啊
lovehongyun 2009-02-10
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication3
{
class TimerExampleState
{
public int counter = 0;
public Timer tmr;
}

class Program
{
static void Main(string[] args)
{
TimerExampleState s = new TimerExampleState();
// Create the delegate that invokes methods for the timer.
TimerCallback timerDelegate = new TimerCallback(CheckStatus);
// Create a timer that waits one second, then invokes every second.
Timer timer = new Timer(timerDelegate, s, 1000, 1000);
// Keep a handle to the timer, so it can be disposed.
s.tmr = timer;

// The main thread does nothing until the timer is disposed.
while (s.tmr != null)
Thread.Sleep(0);
Console.WriteLine("Timer example done.");
}
// The following method is called by the timer's delegate.

static void CheckStatus(Object state)
{
TimerExampleState s = (TimerExampleState)state;
s.counter++;

//写你要执行的命令(执行sql脚本)
Console.WriteLine(s.counter);


//Dispose()
/*
s.tmr.Dispose();
s.tmr = null;
*/
}

}
}

hongqi162 2009-02-10
  • 打赏
  • 举报
回复
asp.net是开发web应用程序的,在web服务器上写个服务定时执行你的sql脚本不就行了么
chenguang79 2009-02-10
  • 打赏
  • 举报
回复
顶起来

110,566

社区成员

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

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

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