当前位置: 首页>>代码示例>>C#>>正文


C# SqlConnection.CanOpen方法代码示例

本文整理汇总了C#中System.Data.SqlClient.SqlConnection.CanOpen方法的典型用法代码示例。如果您正苦于以下问题:C# SqlConnection.CanOpen方法的具体用法?C# SqlConnection.CanOpen怎么用?C# SqlConnection.CanOpen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Data.SqlClient.SqlConnection的用法示例。


在下文中一共展示了SqlConnection.CanOpen方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: OperatorID

        /// <Check Database Connection and operator>a
       /// It consume the Can open function and get the operator ID after validating the credentials.
       /// </Check Database Connection and Operator>
       /// <param name="operatorName"></param>
       /// <returns>integer: operator id</returns>
        public int OperatorID(string operatorName)
      {
          try
          {
              object a;
              string strSecurity = "";

              // Added and changed by usman to make SQL logins work
              if (txtdbserver.Text == "") { return -1; }
              if (txtPassword.Text == "") { strSecurity = ";Integrated Security=True"; }
              string strConn = "Data Source=" + txtdbserver.Text + ";Initial Catalog=" + txtdbName.Text + "; User ID=" + txtUserName.Text + ";Password=" + txtPassword.Text + strSecurity + ";";
              //SqlConnection conn = new SqlConnection(strConn);//old statement before using
              using (var connection = new SqlConnection(strConn)) {
                  if (connection.CanOpen())
                  {
                      connection.Open();
                    // SqlCommand cmd = new SqlCommand("SELECT operator_ID FROM ENTITY.dbo.Operator WHERE [email protected]", connection);
                      SqlCommand cmd = new SqlCommand("SELECT operator_ID FROM tbl_User WHERE [email protected]", connection);
                      cmd.CommandType = CommandType.Text;
                      cmd.CommandTimeout = 5;   //NILS: Needs useful timeout in case doesn't work
                      cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = txtOperatorName.Text;
                      a = cmd.ExecuteScalar();
                      connection.Close();
                      if (string.IsNullOrEmpty(Convert.ToString(a)))
                      {
                          MessageBox.Show("Username doesn't match any operator in the database!", "Cannot proceed", MessageBoxButtons.OK);
                          bool c = false;
                          int b = 0;
                          b = ErrorLog((System.Windows.Forms.Application.StartupPath.ToString() + "/Logs/ErrorLog"), "Username doesn't match any operator in the database!" + "Cannot proceed");
                          switch (b)
                          {
                              case 1:
                                  break;
                              default:
                                  break;
                          }
                          return -1;
                      }// end if
                      else { return Convert.ToInt32(a); }
                  }//end if:CanOpen()
                  else { MessageBox.Show("connection cannot be open is called"); return -1; }                
                }// end using
          }//end try
          catch (Exception ex)
          {
              bool a = false;
              int b = 0;

              b = ErrorLog((System.Windows.Forms.Application.StartupPath.ToString() + "/Logs/ErrorLog"), lblfname.Text + "\t" + ex.Message + "\n" + "Please correct the connection credential!");
              MessageBox.Show("Please correct/provide the database connection credentials!");
              switch (b)
              {
                  case 1:

                      break;
                  default:
                      break;

              }
              return -1;
          }// end catch

      }       
开发者ID:EIS-Bonn,项目名称:MA-INF3232-Lab-SS2014,代码行数:68,代码来源:Form1.cs


注:本文中的System.Data.SqlClient.SqlConnection.CanOpen方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。