C#为连接sql server数据库,写个DBHelper类

Roc posted @ 2010年6月25日 08:01 in C# , 7684 阅读



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace MySchool                                                                     //MySchool是我的项目名称
    class DBHelper
        static string conString = "Data Source=服务器名;Initial Catalog=数据库名;User ID=sa";                                        //我选择的是windows+sql混合验证,登录名sa,如果有Pwd还需要添加
        public static SqlConnection con=new SqlConnection(conString);                                                                                //创建SqlConnection对象,用于连接


using System.Data.SqlClient;
string sql = "select * from teacher";                                                                       //输入的sql命令
SqlCommand cmd = new SqlCommand(sql,DBHelper.con);                        //创建SqlCommand对象,导入sql命令,连接的是DBHelper中写好的数据库
DBHelper.con.Open();                                                                                             //打开数据库
SqlDataReader dr = cmd.ExecuteReader();                                                       //创建SqlDataReader类对象,用于一行一行读取数据库中数据
dr.HasRows                                                                        //在数据库中执行select * from teacher,会有多行受到影响。如果有影响,HasRows返回true,否则返回false
dr.Read();                                                                                //读取一行数据,若想一遍一遍的读,一直到读完为止,则可以写到while语句中
string loginid = dr["LoginID"].ToString();                            //把数据库中字段LoginID的第一个记录给字符串loginid
professional cleanin 说:
2021年9月29日 17:57

Spouse and children cleaning is just not an advantageous undertaking, as well as because the software involves excruciating activities, that include hard scrubbing up, but also while it may entail the installation of products which has been harmful to skin and also the body more often than not. A Norwegian understand, which recognized around 5000 wives mostly going through household chores with nasty chemicals, reported superior occurrences about chronic obstructive pulmonary problem (COPD) and additionally skin irritations in all of these subjects.

登录 *

loading captcha image...
or Ctrl+Enter