简体   繁体   中英

how do I get a cell of data from a specific row in c# with a local sql database file

因此,我几乎搞砸了c#和sql数据库,我创建了一个数据库,该数据库的表有2列,一列用于用户名,一列用于密码,我想知道如何查找用户名所在的行并获取相应的密码(旁边的单元格),因此我可以比较登录aspx页面

Wrong approach. Collect both the username and password, and look to see if there is a match in the database.

How to do it in C#? I like LINQ to SQL, even thought Microsoft recommends otherwise. Create a LINQ to SQL classes object in a C# project, call it MyContextClass , drop your database objects onto it. Then, in your code behind, write something like:

using (MyContextClass ctx = new MyContextClass) {
    if (ctx.SingleOrDefault(f => f.Username == txtUsername.Text && f.Password == txtPassword.Text) != null) {
        // The user got it right.
    }
}

However, all the warnings about storing passwords in the comments above really do apply. Integrating those is left as an exercise for the user.

This is a question that is going to take you doing a bit of research. There are a plethora of tutorials on the internet on how to access data. Below I've included one way that you can return a single value for you to get started with. You'll need to include System.Data.SqlClient.

// See http://www.connectionstrings.com/ for how to build your connection string
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
Object returnValue;

cmd.CommandText = "SELECT COUNT(*) FROM Customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

returnValue = cmd.ExecuteScalar();

sqlConnection1.Close();

I lifted this example from http://msdn.microsoft.com/en-us/library/eeb84awz(v=vs.80).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM