简体   繁体   English

C#SQL从表中选择一个数据

[英]C# SQL Select a data from table

I have a database(username(key),Fullname,Pass), I want to search for the username and write out the Fullname for this username in a console. 我有一个数据库(用户名(密钥),全名,密码),我想搜索用户名并在控制台中写出该用户名的全名。 I have these: 我有这些:

static DataTable dt = new DataTable();
static SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""path\login.mdf"";Integrated Security=True");

con.Open();
string username="mate";
    SqlDataAdapter sda = new SqlDataAdapter("SELECT Nev FROM Login WHERE username='" + username+ "'", con);
        sda.Fill(dt);
        string fullname;
        if (dt.Rows[0][0].ToString() == "1")then?

"Nev" means Full name “ Nev”表示全名

I want it to write the Fullname into the "fullname" string, then just: 我希望它将全名写入“全名”字符串中,然后:

Console.Writeline("Welcome {0}",fullname);

Can you help me with this code? 你能帮我这个代码吗?
I just don't know how to continue my code. 我只是不知道如何继续我的代码。 Users can login and register to the app, and when they login i want to greet them like "Hello FullName" when they register their username, fullname and password is stored, and they login with username & pass. 用户可以登录并注册到该应用程序,当他们登录时,我想在注册用户名,全名和密码时用“ Hello FullName”打招呼,并使用用户名和密码登录。 I just don't know how to find the Fullname if i know the username. 如果我知道用户名,我就是不知道如何查找全名。 (I'm a beginner in SQL) (我是SQL的初学者)

I think this is what you're after: 我认为这是您追求的目标:

string fullname = null;
using(var con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""path\login.mdf"";Integrated Security=True")) {
    string username = "mate";
    SqlCommand cmd = new SqlCommand("SELECT Nev FROM Login WHERE username=@Username", con);
    cmd.Parameters.AddWithValue("@Username", username);

    con.open();
    fullname = cmd.ExecuteScalar()?.ToString();
}

if (fullname == null) {
    Console.WriteLine("The username could not be found!");
}

The using ensures that the connection is cleaned up once the block is exited. using确保在退出该块后清除连接。 Also, you should use Parameters to be more resilient to SQL injection. 另外,您应该使用Parameters来更强地抵抗SQL注入。

For your specific problem, try this code: 对于您的特定问题,请尝试以下代码:

if(dt.Rows.Count > 0)
      fullname = dt.Rows[0]["Nev"].ToString();

But here's a better and complete solution to your problem: 但是,这里有一个更好,更完整的解决方案:

string userName = "mate";
string fullName = string.Empty;

string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=path\login.mdf;Initial Catalog=Login;Integrated Security=True";
string query = "SELECT Nev FROM Login WHERE username=@UserName;";

using (SqlConnection connection = new SqlConnection(connectionString))
{
     SqlCommand command = new SqlCommand(query, connection);
     command.Parameters.Add("@UserName", SqlDbType.NVarChar);
     command.Parameters["@UserName"].Value = userName;

     try
     {
         connection.Open();
         fullName = command.ExecuteScalar().ToString();
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
}

Console.WriteLine("Hello {0}!", fullName);

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

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