本文整理汇总了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
}