如何同时调用两个不同函数的SQL连接方法?
实际上,我正在尝试使用两个不同的函数来调用我的 mysqlconnection 函数,其中一个函数在 TASK 的帮助下在后台连续运行,另一个函数在单击按钮时运行。 当两个函数同时调用 mysqlconnection 函数时,我的代码给出了一个错误。
这是 MYSQL 连接函数代码:
public void executequeryy(CommandType type, string commandtext)
{
try
{
cmd.CommandText = commandtext;
cmd.Connection = PublicClass.conn;
if (type == CommandType.StoredProcedure)
{
cmd.CommandType = CommandType.StoredProcedure;
}
else
{
cmd.CommandType = CommandType.Text;
}
PublicClass.conn.Open();
cmd.ExecuteNonQuery();
PublicClass.conn.Close();
}
catch (Exception ex)
{
PublicClass.conn.Close();
}
}
如果有人有想法,请告诉我如何同时从多个函数调用此连接方法 2 次或更多次。
调用此 mysql 连接函数的示例代码: 连续运行函数:
public async void fillalldata()
{
while(true)
{
await Task.Run(() =>
{
OverallData(); //this function fill data in continuous mode in background.
//await Task.Delay(2000);
});
}
}
这是由按钮单击调用的另一个示例函数:
public void getenvlpdata()
{
DbClass objdata = new DbClass();
objdata.executequeryy(CommandType.Text, "insert into OverallData(SensorID,SampleTime,Temperature,TempUnit,AlarmEnabled,AlarmLevel) values('" + SerialNumber + "','" + SampleTime + "','" + OverallValue + "','" + Unit + "','" + AlarmEnabled + "','" + AlarmLevel + "')");
}
Actually I am trying to call my mysqlconnection function with two different function where one function running continuously in background with the help of TASK and another function run on button click.
My code gives me an error when both function call mysqlconnection function at a same time.
Here is MYSQL connection function code:
public void executequeryy(CommandType type, string commandtext)
{
try
{
cmd.CommandText = commandtext;
cmd.Connection = PublicClass.conn;
if (type == CommandType.StoredProcedure)
{
cmd.CommandType = CommandType.StoredProcedure;
}
else
{
cmd.CommandType = CommandType.Text;
}
PublicClass.conn.Open();
cmd.ExecuteNonQuery();
PublicClass.conn.Close();
}
catch (Exception ex)
{
PublicClass.conn.Close();
}
}
If anyone have an idea please tell me how can I call this connection method 2 or more times from multiple function at a same time.
Example code which are calling this mysql connection fucntion:
continuous run function:
public async void fillalldata()
{
while(true)
{
await Task.Run(() =>
{
OverallData(); //this function fill data in continuous mode in background.
//await Task.Delay(2000);
});
}
}
Here is the another example function which is call by buttonclick:
public void getenvlpdata()
{
DbClass objdata = new DbClass();
objdata.executequeryy(CommandType.Text, "insert into OverallData(SensorID,SampleTime,Temperature,TempUnit,AlarmEnabled,AlarmLevel) values('" + SerialNumber + "','" + SampleTime + "','" + OverallValue + "','" + Unit + "','" + AlarmEnabled + "','" + AlarmLevel + "')");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ADO.NET 不是线程安全的 API;您一次只能与单个逻辑流中的单个连接进行对话;这可以是单个线程,也可以是单个
异步
流,只要它正确await
每个调用,这样事情就不会重叠。看来您在这里只有一个连接(可能还有一个命令,这是一个糟糕的主意);这种情况是不允许的。对单独的流使用单独的连接。
作为旁注(但很重要):
async void
时都ADO.NET is not a thread-safe API; you can only talk to a single connection from a single logical flow at once; this could be a single thread, or a single
async
flow, as long as it properlyawait
s each call so that things are not overlapped. It appears that you have a single connection here (and possibly a single command, which is a terrible idea); this scenario is not allowed.Use separate connections for the separate flows.
As a side note (but important):
async void
, you're probably doing something wrong最后我得到了解决我的问题的方法。
我创建了两个不同的连接字符串。一种用于连续运行功能,第二种用于单次运行(单击按钮后)。
这是两个连接字符串的示例代码:
Finally I got a solution to my problem.
I have created a two different connection string. one for continuous running function and second one is for single run(after click on button).
here is the example code for two connection string: