如何连接数据库
问题描述:
我正在尝试上课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.