Select Column copy another [closed]
Want to improve this question? Add details and clarify the开发者_如何学编程 problem by editing this post.
Closed 9 years ago.
Improve this questionI got this:
+--------+------+
| id | name |
+--------+------+
| 1 | George |
| 2 | Mathew |
| 3 | Michael |
| 4 | Jones |
+--------+------+
Now what I want to do is this.
I want to find my name Michael and copy the id "3" of Michael into a string variable in VS10
You need the following query to select the id
column from the table and specifying the name condition in the WHERE
clause
Select id from table where name = 'Michael'
If you are using Csharp in visual studio 2010, you can do something like this:
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;
In the above code, idValue
is the id
of the name you searching for and connectionString
is a connection string to your database.
You can also use lambda like this:
string idVal = Table.Where(a => a.name== "Michael")
.Select(x => x.Id).FirstOrDefault().ToString();
At first, you need to establish connection between yout application and database using mysql .net connector. This article should help you with that.
Then you should query the database from within your application.
Appropriate SQL query for your request will be "SELECT id FROM table WHERE name = 'Michael'"
精彩评论