如何同时调用两个不同函数的SQL连接方法?

发布于 2025-01-11 17:35:07 字数 1605 浏览 0 评论 0原文

实际上,我正在尝试使用两个不同的函数来调用我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清欢 2025-01-18 17:35:07

ADO.NET 不是线程安全的 API;您一次只能与单个逻辑流中的单个连接进行对话;这可以是单个线程,也可以是单个异步流,只要它正确await每个调用,这样事情就不会重叠。看来您在这里只有一个连接(可能还有一个命令,这是一个糟糕的主意);这种情况是不允许的

对单独的流使用单独的连接。

作为旁注(但很重要):

  1. 参数; 不要连接数据来创建 SQL ,您可能做错了什么
  2. 任何时候输入 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 properly awaits 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):

  1. parameters; do not concatenate data to create SQL
  2. any time you type async void, you're probably doing something wrong
吃→可爱长大的 2025-01-18 17:35:07

最后我得到了解决我的问题的方法。
我创建了两个不同的连接字符串。一种用于连续运行功能,第二种用于单次运行(单击按钮后)。
这是两个连接字符串的示例代码:

    //create connection for continuous running function..
    public static void CreateContinuousConnection() 
    {
        try
        {
            PublicClass.continuousconn.ConnectionString = "datasource = localhost;database=wivib_x;username= root; password=1234";

        }
        catch { }
    }



    //create connection for single running function.
    public static void CreateSingleConnection()
    {
        try
        {
            PublicClass.singleconn.ConnectionString = "datasource = localhost;database=wivib_x;username= root; password=1234";

        }
        catch { }
    }

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:

    //create connection for continuous running function..
    public static void CreateContinuousConnection() 
    {
        try
        {
            PublicClass.continuousconn.ConnectionString = "datasource = localhost;database=wivib_x;username= root; password=1234";

        }
        catch { }
    }



    //create connection for single running function.
    public static void CreateSingleConnection()
    {
        try
        {
            PublicClass.singleconn.ConnectionString = "datasource = localhost;database=wivib_x;username= root; password=1234";

        }
        catch { }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文