因此,我浏览了网络,但找不到任何示例。我有一个登录系统,它检查 sql 数据库以确保信息匹配,然后如果详细信息与数据库中的信息匹配,则让它们进入。但是,我正在努力研究如何通过 WCF 检查数据库。我是 WCF 的新手,我已经设法插入到 wcf,将数据从 WCF 加载到 datagridview 并将 datagridview 保存到 wcf。我是新手,所以如果有人有任何例子,帮助会很大吗?谢谢
这是我在 WCF 之前登录系统的代码
private void BtnLogin_Click(object sender, EventArgs e)
{
{
try
{
SqlConnection sc = new SqlConnection(@"Data Source=localhost;Initial Catalog=Optimisation;Integrated Security=True;Pooling=False"); // sql connection
sc.Open(); // open sql connection
SqlCommand cmd = new SqlCommand("select Username, Password from UserDetails where Username='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "'", sc); // select usernames and passwords
SqlDataReader dr = cmd.ExecuteReader(); // data reader
string Username = txtUsername.Text; // username
string Password = txtPassword.Text; // password
while (dr.Read()) //datareader
{
if ((dr["Username"].ToString() == Username) && (dr["Password"].ToString() == Password)) // if username and passowrd match those in database
{
new Menu().Show(); // show menu form
this.Hide(); // hide this for
}
else
{
MessageBox.Show("Username or Password is invalid", "login error", MessageBoxButtons.OK); // show message box if password or username is invalid
}
}
dr.Close(); // close data reader
sc.Close(); // close sql connection
}
catch
{
MessageBox.Show("Username or Password is invalid", "login error", MessageBoxButtons.OK);
}
}
}
这是我的 WCF 中的内容:
public DataSet SelectUserDetails()
{
SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=WCFTest;Integrated Security=True;Pooling=False");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Username,Password FROM UserDetails", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
cmd.ExecuteNonQuery();
con.Close();
return ds;
}