简体   繁体   English

具有本地SQL Server数据库的Visual Studio登录页面

[英]Visual Studio Login Page with local SQL server database

I'm creating a visual studio project that uses a local SQL server database as a data source, which is up and running correctly. 我正在创建一个Visual Studio项目,该项目使用本地SQL Server数据库作为数据源,该数据库已正常运行。

I need to create a login form for the project. 我需要为该项目创建一个登录表单。

The form has a username textbox and a password textbox which the user will populate with their details, and then hit the 'login' button, which needs to execute the select sql statement. 该表单具有一个用户名文本框和一个密码文本框,用户将使用其详细信息填充该文本框,然后单击“登录”按钮,这需要执行select sql语句。

Any references on how to do this? 有关如何执行此操作的任何参考?

The code I have tried is below. 我尝试过的代码如下。 It's throwing a NullReferenceException at the line that says "SqlDataReader dr = cmd.ExecuteReader();" 它在显示“ SqlDataReader dr = cmd.ExecuteReader();”的行上引发NullReferenceException。

How do I Solve the nullreferenceexception? 我该如何解决nullreferenceexception?

Thank you! 谢谢!

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = "Data Source=MARKO-PC\\SQLEXPRESS;Initial     Catalog=IS2B_G8_FundMeDB;Integrated Security=True";
                con.Open();

                String sql = "Select * from APPLICANT where     applicant_ID_passport =@user AND password = @password";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.Add(new SqlParameter("@user", txtUserName.Text));
                cmd.Parameters.Add(new SqlParameter("@password",     txtPassword.Text));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows == true)
                {
                    MessageBox.Show("Login Successful");
                }
                else
                {
                    MessageBox.Show("Login Failed");
                }
            }
            catch (SqlException sqle)
            {
                MessageBox.Show("Sql Exception");

            }


        }

Try this 尝试这个

string struser = txtUserName.Text;
string strpwd = txtPassword.Text;

 String sql = "Select * from APPLICANT where applicant_ID_passport=" + struser + " AND password = " + strpwd +"";
             SqlCommand cmd = new SqlCommand(sql, con);
             SqlDataReader dr = cmd.ExecuteReader();

You need to do some research into using ADO.Net, specifically the SQLCommand class. 您需要对使用ADO.Net进行一些研究,尤其是SQLCommand类。

However I would refrain from using inline sql statements like above as this opens you up to SQL injection. 但是,我将避免使用上述内联sql语句,因为这可以使您进行SQL注入。 Rather use paramaterised queries, stored procedures or LINQ to SQL. 而是使用参数化查询,存储过程或LINQ to SQL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Visual Studio 中使用本地数据库创建登录应用程序 - Create a login application with local database in visual studio 插入登录和注册数据未显示在 SQL 服务器数据库表 Visual Studio 2019 - Insert login and registration data not show in SQL Server database table Visual Studio 2019 如何使用Visual Studio 2015连接到ASP.NET中的本地SQL Server数据库? - How to connect to a local SQL Server database in ASP.NET using Visual Studio 2015? ASP.NET本地SQL Server数据库C#gridview数据绑定Visual Studio 2013 - ASP.NET local SQL Server database C# gridview data binding Visual Studio 2013 如何使用Visual Studio 2013连接到asp.net中的本地SQL Server数据库? - How to connect to a local SQL server database in asp.net using Visual Studio 2013? 如何在Visual Studio 2017中从C#代码制作SQL Server 2017本地数据库? - How to make a SQL Server 2017 local database from C# code in Visual Studio 2017? 在尝试使用C#连接到Visual Studio 2010中的本地SQL Server数据库时遇到问题 - Having problems trying to connect to local SQL Server database in Visual Studio 2010 using C# Visual Studio和SQL Server。 无法将数据库导入VIsual Studio - Visual Studio and SQL Server . Cannot see Database into VIsual Studio 在Visual Studio中创建SQL Server数据库时出错 - Error creating an SQL Server Database in Visual Studio 在Visual Studio中使用SQL Server中的数据库 - Using Database from SQL Server in Visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM