开发者

Select Column copy another [closed]

开发者 https://www.devze.com 2023-03-30 15:19 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the开发者_如何学编程 problem by editing this pos
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the开发者_如何学编程 problem by editing this post.

Closed 9 years ago.

Improve this question

I 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 connectionStringis 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'"

0

精彩评论

暂无评论...
验证码 换一张
取 消