选择“复制另一个列”
本文关键字:另一个 复制 选择 | 更新日期: 2023-09-27 18:06:35
我明白了:
+--------+------+
| 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;
在上面的代码中,idValue
是您搜索的名称的id
, connectionString
是到您的数据库的连接字符串。
你也可以这样使用lambda:
string idVal = Table.Where(a => a.name== "Michael")
.Select(x => x.Id).FirstOrDefault().ToString();
首先,您需要使用mysql .net连接器在应用程序和数据库之间建立连接。这篇文章应该能帮到你。
那么您应该从应用程序中查询数据库。适合您的请求的SQL查询将是"SELECT id FROM table WHERE name = 'Michael'"