[英]SqlDataReader not reading data from database
我正在嘗試為我的項目設置登錄名,因此我正在使用SqlCommand
進行命令,然后使用SqlDataReader
從數據庫中讀取數據,但是每當我嘗試讀取它時,閱讀器就會顯示為空。
當我調試該應用程序時,我注意到該連接未顯示帶有密碼的完整版本,並且我四處搜索並發現出於安全原因它並未顯示該版本,因此我嘗試使用Persist Security Info = true
但這沒用。
這是我的聯系:
SqlConnection connection = new SqlConnection("Server=.\\SQLEXPRESS;Database=BD_GA;Uid=sa;Pwd=SQL_;Persist Security Info=True");
我的登錄代碼:
connection.Open();
SqlCommand commandLogin = new SqlCommand("select * from Proprietário", connection);
SqlDataReader reader = commandLogin.ExecuteReader();
while (reader.Read())
{
if (reader.GetValue(5).ToString() == txtUser.Text && reader.GetValue(6).ToString() == txtPass.Text)
{
User = txtUser.Text;
tipo = reader.GetBoolean(7);
tssl_Login.Text = "Login feito com sucessso!";
break;
}
}
如果可以,它應該顯示“ Login feito com su sucesso”,但它什么也沒有顯示。
請在下面嘗試這些詳細信息
連接
SqlConnection connection = new SqlConnection("Data Source = .\\SQLEXPRESS;
Database = BD_GA;User ID = sa;Password = SQL_;Integrated Security = true;
Encrypt = False; TrustServerCertificate = True; ApplicationIntent = ReadWrite; MultiSubnetFailover = False");
如果連接有問題,將顯示錯誤消息。
try
{
connection.Open();
SqlCommand commandLogin = new SqlCommand("select * from Proprietário", connection);
SqlDataReader reader = commandLogin.ExecuteReader();
while (reader.Read())
{
if (reader.GetValue(5).ToString() == txtUser.Text && reader.GetValue(6).ToString() == txtPass.Text)
{
User = txtUser.Text;
tipo = reader.GetBoolean(7);
tssl_Login.Text = "Login feito com sucessso!";
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.