English 中文(简体)
如何检查各州和决定执行代码的哪一部分
原标题:How to check states and decide which part of the code to execute

我有这个密码:

//open database connection on the server
                con = new MySqlConnection("server=localhost;database=GPS;uid=root;pwd=****");
                con.Open();
                if (con.State == ConnectionState.Closed)
                {
                    cmd = new MySqlCommand("insert into data values (null, ?Parname , ?Parname2, ?Parname3, ?Parname4, ?Parname5, ?Parname6, ?Parname7);", con);
                    cmd.Parameters.Add("?Parname", MySqlDbType.Double).Value = Math.Round(deciLat, 5);
                    cmd.Parameters.Add("?Parname2", MySqlDbType.Double).Value = Math.Round(deciLon, 5);
                    cmd.Parameters.Add("?Parname3", MySqlDbType.Timestamp).Value = DateTime.Now;
                    if (card_number != null)
                    {
                        cmd.Parameters.Add("?Parname4", MySqlDbType.VarChar).Value = card_number;
                    }
                    else
                    {
                        cmd.Parameters.Add("?Parname4", MySqlDbType.VarChar).Value = null;
                    }
                    cmd.Parameters.Add("?Parname5", MySqlDbType.VarChar).Value = ConfigSettings.ReadSetting("reg");
                    cmd.Parameters.Add("?Parname6", MySqlDbType.VarChar).Value = ConfigSettings.ReadSetting("ser");
                    cmd.Parameters.Add("?Parname7", MySqlDbType.Double).Value = ellipHeight;
                    cmd.ExecuteNonQuery();
                    lastDBUpdate = DateTime.Now;
                }
                else
                {
                    // local mysql database
                    con = new MySqlConnection("server=localhost;database=temp_GPS;uid=root;pwd=******");
                    con.Open();
                    if (con.State == ConnectionState.Open)
                    {
                        cmd = new MySqlCommand("insert into temp_data values (null, ?Parname , ?Parname2, ?Parname3, ?Parname4, ?Parname5, ?Parname6, ?Parname7);", con);
                        cmd.Parameters.Add("?Parname", MySqlDbType.Double).Value = Math.Round(deciLat, 5);
                        cmd.Parameters.Add("?Parname2", MySqlDbType.Double).Value = Math.Round(deciLon, 5);
                        cmd.Parameters.Add("?Parname3", MySqlDbType.Timestamp).Value = DateTime.Now;
                        if (card_number != null)
                        {
                            cmd.Parameters.Add("?Parname4", MySqlDbType.VarChar).Value = card_number;
                        }
                        else
                        {
                            cmd.Parameters.Add("?Parname4", MySqlDbType.VarChar).Value = null;
                        }
                        cmd.Parameters.Add("?Parname5", MySqlDbType.VarChar).Value = ConfigSettings.ReadSetting("reg");
                        cmd.Parameters.Add("?Parname6", MySqlDbType.VarChar).Value = ConfigSettings.ReadSetting("ser");
                        cmd.Parameters.Add("?Parname7", MySqlDbType.Double).Value = ellipHeight;
                        cmd.ExecuteNonQuery();
                        lastDBUpdate = DateTime.Now;
                    }
                }

如果例如连接状态正常, 如果连接状态正常, 服务器已建立连接, 我想使我的代码工作, 然后在服务器上保存结果, 如果连接到服务器失败, 然后连接到本地数据库, 并且保存到本地数据库。 本地数据库总是可以访问的 。

有人能安排我的代码 工作,因为我想,我不知道 如何实现它。

谢谢

最佳回答

创建 GetConnectry () 函数, 并使用此函数获取连接 。

写下代码以检查连接状态, 并以此为基础返回连接 。

public MySqlConnection GetConnection(MySqlConnection con)
{
        if (con.State == ConnectionState.Closed)
       {
           // local mysql database
             con = new MySqlConnection("server=localhost;database=temp_GPS;uid=root;pwd=******");
            con.Open();
       }

          return con;
    }
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签