C# 选择“复制另一个”列

C# 选择“复制另一个”列,c#,mysql,visual-studio-2010,C#,Mysql,Visual Studio 2010,我明白了: +--------+------+ | id | name | +--------+------+ | 1 | George | | 2 | Mathew | | 3 | Michael | | 4 | Jones | +--------+------+ 现在我想做的就是这样 我想找到我的名字Michael并将Michael的id“3”复制到VS10中的字符串变量中您需要以下查询从表中选择id列,并在WHERE子句中指定名称条件 Selec

我明白了:

+--------+------+
| id  | name    |
+--------+------+
| 1   | George  |  
| 2   | Mathew  |  
| 3   | Michael | 
| 4   | Jones   |
+--------+------+
现在我想做的就是这样


我想找到我的名字Michael并将Michael的id“3”复制到VS10中的字符串变量中

您需要以下查询从表中选择
id
列,并在
WHERE
子句中指定名称条件

Select id from table where name = 'Michael'
如果您在visual studio 2010中使用Csharp,可以执行以下操作:

string idValue = String.Empty
string query = " Select id from table where name = 'Michael'";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader;
connection.Open();
reader = command.ExecuteReader();
While(reader.Read())
{
idValue = reader["id"].ToString();
}
connection.Close();
return idValue;
string idVal = Table.Where(a => a.name== "Michael")
               .Select(x => x.Id).FirstOrDefault().ToString();
在上面的代码中,
idValue
是您搜索的名称的
id
,而
connectionString
是到数据库的连接字符串

您也可以像这样使用lambda:

string idValue = String.Empty
string query = " Select id from table where name = 'Michael'";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader;
connection.Open();
reader = command.ExecuteReader();
While(reader.Read())
{
idValue = reader["id"].ToString();
}
connection.Close();
return idValue;
string idVal = Table.Where(a => a.name== "Michael")
               .Select(x => x.Id).FirstOrDefault().ToString();

首先,您需要使用mysql.net连接器在您的应用程序和数据库之间建立连接。 这篇文章应该对你有所帮助

然后,您应该从应用程序中查询数据库。
对于您的请求,合适的SQL查询将是
“从表中选择id,其中name='Michael'”

使用实体框架,如selectid WHERE name=“Michael”?你可以先回去和你的老师谈谈。。。