程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> sql-C#如何將combobox裡面我選擇的項和表內的一個數值比對,如果相符則進入對應的頁面。

sql-C#如何將combobox裡面我選擇的項和表內的一個數值比對,如果相符則進入對應的頁面。

編輯:編程綜合問答
C#如何將combobox裡面我選擇的項和表內的一個數值比對,如果相符則進入對應的頁面。

我們數據庫課程設計做學生成績管理系統。
我現在在做登錄界面的代碼,對應的表結構是user_name,user_pwd,status遇到了一個問題:就是只要我輸入的用戶名和密碼正確,在combobox裡面選擇管理員就進入管理員頁面,選擇學生就進入了學生頁面。表裡面的status是標注這個賬號是什麼角色,想在代碼裡加上一個條件就是希望能將combobox裡面我選擇的項和表內的status比對,如果相符則進入對應的頁面。

下面是我登錄部分的代碼:
public bool CheckInput()
{
bool flag=false;
if(string.IsNullOrEmpty(this.用戶名.Text.Trim()))
{
MessageBox.Show("用戶名不能為空,登錄失敗","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else if(string.IsNullOrEmpty(this.密碼.Text.Trim()))
{
MessageBox.Show("密碼不能為空,登錄失敗","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else
{
flag=true;
}
return flag;
}
///
/// 登錄按鈕點擊事件
///
///
///
private void 登錄_Click(object sender, EventArgs e)
{
if (CheckInput())
{
//無密碼建立連接SQL數據庫對象
string connStr = "Data Source=PC-20140923RVHL\SQL2005;Initial Catalog=Stu_mark_MS;Integrated Security=True";
//創建數據庫連接對象
SqlConnection conn = new SqlConnection(connStr);
try
{
//打開數據庫連接
conn.Open();
//創建SQL語句
string sql = "select user_name,user_pwd,status from user_info where user_name='" + 用戶名.Text + "'and user_pwd='" + 密碼.Text + "'";
//創建命令對象
SqlCommand comm = new SqlCommand(sql, conn);//執行SQL命令
comm.CommandText = sql;
comm.Connection = conn;
SqlDataReader ada = comm.ExecuteReader();
if (ada.Read())
{
if (this.身份.SelectedItem.ToString() == "管理員"&& status=="管理員")
{
//創建管理員信息窗體實例
admin admin = new admin();
//打開管理員信息窗體
admin.Show();
}
if (this.身份.SelectedItem.ToString() == "學生")
{
//創建學生信息窗體實例
student student = new student();
//打開學生信息窗體
student.Show();
}
}
else
{
MessageBox.Show("用戶名密碼有誤,請重新輸入");
}
}
catch (Exception ex)
{
MessageBox.Show("出現異常:" + ex.Message);
}
finally
{
conn.Close();
}
}

最佳回答:


select user_name,user_pwd,status from user_info where user_name='" + 用戶名.Text + "'and user_pwd='" + 密碼.Text + "' and status =‘" +combobox.text+",";
然後直接飯後受影響行數 comm.ExecuteNonQuery();然後判斷行數>0就竟然combox對應的界面否則提示錯誤

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved