c#链接数据库
时间:2021-07-01 10:21:17
帮助过:2人阅读
System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 数据库连接
{
class Program
{
static void Main(
string[] args)
{
//连接数据库
SqlConnection conn =
new SqlConnection(
"server=.;database=cangkudata;user=sa;pwd=1243672715");
SqlCommand cmd = conn.CreateCommand();
//创建数据库操作对象
cmd.CommandText =
"select * from cangku ";
//指令
conn.Open();//打开数据库连接
SqlDataReader dr = cmd.ExecuteReader();
//返回一个dr结果值
if (dr.HasRows)
//判断是否含有数据,返回true or false
{
while (dr.Read())
//指针下移
{
Console.Write(dr[0].ToString() +
" " + dr[
1].ToString() +
"\n");
}
}
conn.Close();
Console.ReadKey();
}
}
}
c#链接数据库
标签: