C#登录页面.SQL Serversda.Fill(dt);& quot;错误 [英] C# Login page. SQL Server " sda.Fill(dt); " ERROR

查看:75
本文介绍了C#登录页面.SQL Serversda.Fill(dt);& quot;错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在观看此视频上了解更多信息.还有一件事..使用后应该关闭SqlConnection.

I was watching this video https://www.youtube.com/watch?v=tcmmCcMs8yU . It basically teaches me how to create my own login page using SQL Server.

So after following exactly what he did, when I click the submit button I have an error highlighting sda.Fill(dt);. I am pretty new to SQL Server, please advise!

My code:

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Glenntdy\Documents\GlennTeoDB.mdf;Integrated Security=True;Connect Timeout=30");        

SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From Table where Username='"+txtName.Text + "' and Password = '" +txtPassword.Text + "'",con);

DataTable dt = new DataTable();

sda.Fill(dt);

if (dt.Rows[0][0].ToString() == "1")
{
    this.Hide();

    Main ss = new Main();
    ss.Show();
}
else
{
    MessageBox.Show("Please double check your Username and password");
}

Picture of error:

I am unable to post picture due to insufficient reputation

Thanks in advance!

EDIT : The main issue have been solved, thanks to all!

Hopefully I am not breaking any rules but does anybody know how to change the password text to * while typing instead of showing just the plain password? Just any other website login page? - Issue have been solved by Sohaiby. Thanks a lot!

EDIT[2] : The question have been solved!

解决方案

Table is a reserved keyword and should be surrounded by braces like [Table]. Additionally it is not recommended to construct your query the way you do, because of SqlInjection. Read more on SqlParameters. One more thing.. you should close the SqlConnection after use.

这篇关于C#登录页面.SQL Serversda.Fill(dt);& quot;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆