简体   繁体   English

如何将 SQL 数据添加到变量中?

[英]How to add SQL data into variable?

How can I add the SQL data into a variable in C#?如何将 SQL 数据添加到 C# 中的变量中? Thanks for your help.谢谢你的帮助。

 string user = textBox1.Text;
    string password = textBox2.Text;
    con = new SqlConnection("...");//actually, there is a connection string but because of security, I'm not sharing it with you.
    com = new SqlCommand();
    con.Open();
    com.Connection = con;
    com.CommandText = "Select*From login where username='" + textBox1.Text +
        "'And sifre='" + textBox2.Text + "'";
    dr = com.ExecuteReader();
    if (dr.Read())
    {
      
    }

It is my login code it will open a new form but first I should find the username data of it and put it into a variable.这是我的登录代码,它将打开一个新表单,但首先我应该找到它的用户名数据并将其放入一个变量中。

Here is a pattern to follow, place your data operations in a separate class and call in your form.这是要遵循的模式,将您的数据操作放在单独的 class 中,然后调用您的表单。 Here Login class could be whatever you want to call it with required properties.这里Login class 可以是任何你想用所需属性调用它的东西。

public class Login
{
    public int Id { get; set; }
    . . .
}

Mocked data class, here since select * is used rather than specifying only required columns I assume the first column is the primary key which it may or may not be so adjust as needed and add other columns/properties to the Login class instance.模拟数据 class,这里使用select *而不是仅指定必需的列我假设第一列是主键,它可能会或可能不会根据需要进行调整,并将其他列/属性添加到Login class 实例。

public class DataOperations
{
    // caller validates parameters have values
    public static Login DoSomething(string userName, string sifre)
    {
        Login login = new Login();

        var selectStatement = "SELECT * FROM login WHERE username= @userName AND sifre = @sifre";

        using (var cn = new SqlConnection("TODO"))
        {
            using (var cmd = new SqlCommand(selectStatement, cn))
            {
                cmd.Parameters.Add("@userName", SqlDbType.NVarChar).Value = userName;
                cmd.Parameters.Add("@sifre", SqlDbType.NVarChar).Value = sifre;

                cn.Open();

                var reader = cmd.ExecuteReader();

                if (!reader.HasRows) return login;
                reader.Read();
                login.Id = reader.GetInt32(0);
            }
        }

        return login;
    }
}

Call the method above in your form在您的表单中调用上面的方法

Login login = DataOperations.DoSomething("TODO", "TODO");

Karen's code, using Dapper (right click the project, choose Manage Nuget Packages, click Browse, type Dapper): Karen 的代码,使用 Dapper(右击项目,选择 Manage Nuget Packages,点击 Browse,输入 Dapper):

public static Login DoSomething(string userName, string sifre)
{
    using var c = new SqlConnection("conn str here");
    var id = c.ExecuteScalar<int?>(
        "SELECT id FROM login WHERE username= @u AND sifre = @s", 
        new{ u = textBox1.Text, s = textBox2.Text }
    );

    return new Login() { Id = id ?? 0 };
}

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

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