本文介绍了如何从Sql C#读取特定用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 请参阅下面的代码和问题。 使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 使用 System.Threading.Tasks; 使用 System.Data.SqlClient; 命名空间 EQCasTest { class Program { 静态 void Main( string [] args) { SqlConnection myConnection = new SqlConnection( user id = username; + @ password = password; server = test-pc\sqlexpress; + Trusted_Connection = yes; + database = eqcas; + connection timeout = 10); 尝试 { myConnection.Open(); } catch (例外e) { Console.WriteLine(e.ToString()); } 尝试 { SqlDataReader myReader = null ; SqlCommand myCommand = new SqlCommand( select *来自cas_user_ext, myConnection); myReader = myCommand.ExecuteReader(); while (myReader.Read()) { Console.WriteLine(myReader [ fullname]。ToString()); Console.WriteLine(myReader [ email]。ToString()); Console.WriteLine(myReader [ x_id]。ToString()); myReader.NextResult(); } } catch (例外e) { Console.WriteLine(e.ToString( )); } } } } 所以我的问题是我怎么会如果数据库包含以下数据,则检索特定用户fullnameemailx_iddepartmentpassword。示例,如果我想检索Nelsons数据但只知道他的用户名。 [x_id] [用户名] [姓名] [电子邮件] [部门] [密码] 501,johnr,John Rambo,john.rambo @ test.com,IT,密码 502,nelsonm1,Nelson Mandela,nelson.m @ test.com,HR,密码1 503,jblack,Joe Black,joe.black @ test.com,销售,密码2 关于我的代码的任何其他建议我将不胜感激:D不想从一开始就接受'坏习惯'解决方案 Please see code and question below.using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SqlClient; namespace EQCasTest{ class Program { static void Main(string[] args) { SqlConnection myConnection = new SqlConnection("user id=username;" + @"password=password;server=test-pc\sqlexpress;" + "Trusted_Connection=yes;" + "database=eqcas; " + "connection timeout=10"); try { myConnection.Open(); } catch (Exception e) { Console.WriteLine(e.ToString()); } try { SqlDataReader myReader = null; SqlCommand myCommand = new SqlCommand("select * from cas_user_ext", myConnection); myReader = myCommand.ExecuteReader(); while (myReader.Read()) { Console.WriteLine(myReader["fullname"].ToString()); Console.WriteLine(myReader["email"].ToString()); Console.WriteLine(myReader["x_id"].ToString()); myReader.NextResult(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } } }}So my question is how would i retrieve a specific users "fullname" "email" "x_id" "department" "password" if the database contained the following data. Example if i wanted to retrieve Nelsons data but only know his username.[x_id] [username] [fullname] [email] [department] [password]501, johnr, John Rambo, [email protected], I.T, password502, nelsonm1, Nelson Mandela, [email protected], HR, password1503, jblack, Joe Black, [email protected], Sales, password2Any other advice on my code in general would be much appreciated :D don't want to pick up 'bad habits' from the start 解决方案 这篇关于如何从Sql C#读取特定用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 14:45