Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#打开计算机后出现连接问题_C#_Connection_Odbc_Sqlanywhere - Fatal编程技术网

C#打开计算机后出现连接问题

C#打开计算机后出现连接问题,c#,connection,odbc,sqlanywhere,C#,Connection,Odbc,Sqlanywhere,我想将用C#编写的应用程序与SQLAnywhere 17数据库连接起来。通过直接引用数据库驱动程序,我可以使用OdbcConnection类 我的例子是: private void butODBC3_Click(object sender, EventArgs e) { string DBase = "FTG_001"; string Host = "192.168.13.4"; if (testODBC(DBase, Host) ==

我想将用C#编写的应用程序与SQLAnywhere 17数据库连接起来。通过直接引用数据库驱动程序,我可以使用OdbcConnection类

我的例子是:

private void butODBC3_Click(object sender, EventArgs e)
    {
        string DBase = "FTG_001";
        string Host = "192.168.13.4";

        if (testODBC(DBase, Host) == true)
        {
            labODBC.BackColor = Color.GreenYellow;
        }
        else
        {
            labODBC.BackColor = Color.Red;
        }
    }

    private bool testODBC(string DBase, string host)
    {
        string connectionString = @"Driver={SQL Anywhere 17};DatabaseName=" + DBase + ";EngineName=monwin;uid=monodbc;pwd=monodbc;LINKS=tcpip(ClientPort=5050-5060;HOST=" + host + ";PORT=2020;DoBroadcast=NONE)";
        try
        {
            using (OdbcConnection connection = new OdbcConnection(connectionString))
            {
                connection.Open();
                OdbcCommand command = new OdbcCommand("select getDate()", connection);
                command.ExecuteNonQuery();

                return true;
            }
        }
        catch (Exception)
        {
            return false;
        }
    }
单击按钮时,如果存在连接,标签颜色将变为绿色;如果存在问题,标签颜色将变为红色

一切正常,但有一个条件。。。打开计算机后,我需要单击ODBC数据源管理器中的测试连接,以使程序正常工作。否则,它将不会连接到数据库

如何在ODBC数据源管理器中强制执行相同的连接测试??同样有效


谢谢您的建议。

您发布的代码应该可以使用。我会在代码中添加一个断点,以确保它实际到达了代码。另一种可能是ODBC数据源管理器中的连接字符串与发布的代码不同。
打开计算机后,我需要单击ODBC数据源管理器中的测试连接以使程序正常工作如果不这样做,抛出的异常是什么?小更改。。。连接到数据库的问题是每次我失去与网络的连接并再次连接时。下面是异常:捕获的异常:System.Data.dll中的“System.Data.Odbc.OdbcException”(“未找到错误[08001][SAP][Odbc驱动程序][SQL Anywhere]数据库服务器”)。捕获到异常:System.Data.dll中的“System.Data.Odbc.OdbcException”(“未找到错误[08001][SAP][Odbc驱动程序][SQL Anywhere]数据库服务器”)