Databinding in WPF (C#)
sorry for any inconveniece caused.
Would like to ask about database or rather databinding
My Window look like this: http://i25.tinypic.com/a0x5kn.jpg
The browse button is look up the开发者_JAVA技巧 "My Network Place"
private void browseBtn_Click(object sender, RoutedEventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
Type t = dialog.GetType();
System.Reflection.FieldInfo fi = t.GetField("rootFolder", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
fi.SetValue(dialog, 0x0012); dialog.ShowDialog();
dialog.ShowDialog();
string selected = dialog.SelectedPath;
computerText.Text = dialog.SelectedPath;
}
So now my computerText.Text pass info, for eg: Computer1
Now for the login, I want a database to see whether the info of the computerText is under which username and password. I have already created a database using Microsoft Access which look like this...
id computerName loginID password
1 Computer1 com1 123456
2 Computer2 com2 234567
So i roughly type the command for the loginBtn...
private void loginBtn_Click(object sender, RoutedEventArgs e)
{
// if the computerText.Text == my datasource's data [computerName] && loginName.Text == my datasource's data [loginID] && password.Text == my datasource's data [password]
{
loginStatus.Text = "Login Success!";
}
else
{
loginStatus.Text = "Login Failed!";
}
So my question is:
1) How to bind my database that I have now?
2) how to code the above codes that I wanted to do?
Help needed =[
Updated Have done the following code:
private void loginBtn_Click(object sender, RoutedEventArgs e)
{
OleDbConnection conn;
OleDbCommand comm;
OleDbDataReader dr;
conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/Database1.accdb");
comm = new OleDbCommand("Select * from Remote where loginId =@id and password=@pass", conn);
comm.Parameters.Add(new OleDbParameter("@id",System.Data.oleDbType.NVarChar,20, "loginID"));
comm.Parameters.Add(new OleDbParameter("@pass",System.Data.oleDbType.NVarChar,20, "password"));
conn.Open();
dr = comm.ExecuteReader();
conn.Close();
if (dr.HasRows)
{
loginStatus.Text = "Login Successfully";
}
else
{
loginStatus.Text = "Login Failed!";
string message = "Wrong username/password is been entered!";
string caption = "Alert!";
MessageBoxButton buttons = MessageBoxButton.OK;
MessageBoxImage icon = MessageBoxImage.Warning;
System.Windows.MessageBox.Show(message, caption, buttons, icon);
}
}
Prompt error: "The type or namespace name 'oleDbType' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)"
And if i Remove the System.Data.oleDbType.NVarChar,20, it works perfectly fine, but when I type according to my database in my username and password, it prompt me Login Failed.. EG:
Computer Name : Computer1
Username: com1
Password: 123456
fMay be something like this, create a connection and than read from your access file.
OleDbConnection conn;
OleDbCommand comm;
OleDbDataReader dr;
conn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\\db1.mdb");
comm = new OleDbCommand("Select * from Table1 where loginId =@id and password=@pass",conn);
comm.Parameters.Add(new OleDbParameter("@id",System.Data.oleDbType.NVarChar,20, "loginID"));
comm.Parameters.Add(new OleDbParameter("@pass",System.Data.oleDbType.NVarChar,20, "password"));
conn.Open();
dr = comm.ExecuteReader();
conn.Close();
if(dr.hasrows)
{
//login success
}
else
//login fail
Than you can authenticate by using dr against the user input. Put this above your commented line.
精彩评论