2023年6月21日发(作者:)
VS2010使⽤c#连接SQLServer数据库准备⼯作: 准备好vs2010,sqlSERVER2008的安装包,如果需要的话,留下邮箱我发给你第⼀步:安装VS2010
第⼆步:安装SQLServer2008 注:这⾥也可以选择默认实例
第三步:使⽤c#连接SQLServer数据库
使⽤SQLServer的管理⼯具登录数据库,选择sql server⾝份验证,⽤户名填写sa 密码是刚才安装过程中输⼊的密码新建⼀个数据库,这⾥我新建了⼀个mysql数据库,然后新建⼀个表student
表的结构如下,有姓名sname, 学号 sno, 性别 sex , 年龄 age 右键表,编辑前200⾏,输⼊数据 新建⼀个查询,查看数据是否写⼊成功use mysqlselect sname ,sno,sex,age from student 点击执⾏,结果如下: 使⽤c#连接数据库,新建⼀个c#控制台应⽤程序using System;using ;using ent;using c;using ;using ;namespace 连接数据库{ class Program { static void Main(string[] args) { //指定Sql Server提供者的连接字符串 string connString = "server=WIN-JUKODHILVDPSQLEXPRESS;database =mysql;uid=sa;pwd=123456"; //建⽴连接对象 SqlConnection Sqlconn = new SqlConnection(connString); //打开连接 (); //为上⾯的连接指定Command对象 SqlCommand thiscommand = Command(); dText = "select sname,sno,sex,age from student"; //为指定的command对象执⾏DataReader SqlDataReader thisSqlDataReader = eReader(); while (()) { ine("{0} {1} {2} {3}", thisSqlDataReader["sname"], thisSqlDataReader["sno"], thisSqlDataReader["sex"], thisSqlDataReader["age"]); } //关闭读取 (); //关闭连接 (); ne(); } }}
注意:服务器名 sever有的话,要使⽤转义字符
运⾏结果如下:总结:1.建⽴与数据库的连接
2.查询数据 3.关闭连接
2023年6月21日发(作者:)
VS2010使⽤c#连接SQLServer数据库准备⼯作: 准备好vs2010,sqlSERVER2008的安装包,如果需要的话,留下邮箱我发给你第⼀步:安装VS2010
第⼆步:安装SQLServer2008 注:这⾥也可以选择默认实例
第三步:使⽤c#连接SQLServer数据库
使⽤SQLServer的管理⼯具登录数据库,选择sql server⾝份验证,⽤户名填写sa 密码是刚才安装过程中输⼊的密码新建⼀个数据库,这⾥我新建了⼀个mysql数据库,然后新建⼀个表student
表的结构如下,有姓名sname, 学号 sno, 性别 sex , 年龄 age 右键表,编辑前200⾏,输⼊数据 新建⼀个查询,查看数据是否写⼊成功use mysqlselect sname ,sno,sex,age from student 点击执⾏,结果如下: 使⽤c#连接数据库,新建⼀个c#控制台应⽤程序using System;using ;using ent;using c;using ;using ;namespace 连接数据库{ class Program { static void Main(string[] args) { //指定Sql Server提供者的连接字符串 string connString = "server=WIN-JUKODHILVDPSQLEXPRESS;database =mysql;uid=sa;pwd=123456"; //建⽴连接对象 SqlConnection Sqlconn = new SqlConnection(connString); //打开连接 (); //为上⾯的连接指定Command对象 SqlCommand thiscommand = Command(); dText = "select sname,sno,sex,age from student"; //为指定的command对象执⾏DataReader SqlDataReader thisSqlDataReader = eReader(); while (()) { ine("{0} {1} {2} {3}", thisSqlDataReader["sname"], thisSqlDataReader["sno"], thisSqlDataReader["sex"], thisSqlDataReader["age"]); } //关闭读取 (); //关闭连接 (); ne(); } }}
注意:服务器名 sever有的话,要使⽤转义字符
运⾏结果如下:总结:1.建⽴与数据库的连接
2.查询数据 3.关闭连接
发布评论