我真的想要一个关于这段代码的帮助,尝试将SQL与C#连接起来制作一个windows表单来填充密码和用户名,我dt.Fill();给出了一个错误,非创新成员...我想知道我应该对代码做些什么?
namespace Homeworkc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=C:\Users\courageboy\AppData\Local\Temporary Projects\Homeworkc\Data.sdf");
SqlDataAdapter sda = new SqlDataAdapter("Select Count(*)From Login where username='" + textBox1.Text + "'and password='" + textBox2.Text + "'", con);
DataTable dt = new DataTable();
dt.Fill();
if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
Displayform df = new Displayform();
df.Show();
}
else
{
MessageBox.Show("please you have enter a wrong Username or Password");
}
}
}
}
答案 0 :(得分:3)
使用SqlDataAdapter
,您需要填写DataTable
,如:
sda.Fill(dt);
这使用方法SqlDataAdapter.Fill Method
发表声明:
DataTable dt = new DataTable();
dt.Fill(); //this one
此DataTable
与SqlDataAdapter
无关,您需要调用SqlDataAdapter.Fill
方法填充DataTable
。
作为不考虑在代码中使用Parameters的一方,您当前的代码向Sql Injection开放