如何连接数据库 [英] how to connect with databases
本文介绍了如何连接数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试上课C#,我想连接数据库,不是不喜欢用PHP编写代码。有人可以建议我吗?
Hi, i'm try to lesson C# and i want to connect with database, is't no like i making code with php. can someone suggeest me ?
推荐答案
尝试:
Try:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT iD, description FROM myTable", con))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int id = (int) reader["iD"];
string desc = (string) reader["description"];
Console.WriteLine("ID: {0}\n {1}", iD, desc);
}
}
}
}
显然,您需要连接字符串,并使用自己的列和表名。
Obviously, you will need your connection string, and to use your own column and table names.
这篇关于如何连接数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文