[英]Can't add table to SQL Server database in Visual Studio 2015
[英]Interacting with SQL Server database in Visual Studio 2015
我意識到這個問題可能涉及面很廣,但是我一直在尋找過去兩天沒有運氣的問題。
我已經在Visual Studio 2015中創建了一個項目,並使用通過SQL Server Management Studio創建的雲數據庫在該項目中創建了新的數據源。 我現在正在嘗試對項目中的登錄頁面進行編碼,該頁面將從UI上的2個文本框中獲取用戶名和密碼,並執行SQL查詢以檢查用戶是否存在於數據庫中以及密碼是否正確。 我熟悉用於數據庫連接的Java代碼,例如准備好的語句和結果集函數。 C#是否有等效的東西?
謝謝,參考有用的文章或代碼示例將不勝感激。
這可能是最快但不是最好的檢查方法。
string yourConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Users.accdb; Persist Security";
using (OleDbConnection conn = new OleDbConnection(yourConnectionString))
{
try
{
conn.Open();
using (OleDbCommand cmd = new OleDbCommand("Select * from UsersTable where UName = @Username and Pass = @Password"))
{
cmd.Parameters.AddWithValue("@Username", txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPass.Text);
using (OleDbDataReader r = cmd.ExecuteReader())
{
if (r.HasRows)
{
Console.WriteLine("User exists")
}
else
{
Console.WriteLine("User does not exist")
}
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.