.NET中SQLServer数据库的安装和入门使用
安装数据库
里安装64位的SQL Server 2008 R2,
下载地址
http://www.microsoft.com/en-us/download/details.aspx?id=23650
双击exe,选择“全新安装”
然后安装所有功能
命名实例:doesthismatter,
实例id :legion,实例文件目录C:\sqlserverfiles,
设置混合模式密码:123456
。登录使用,服务器名称localhost\doesthismatter
登录名:sa,密码123456
附加数据库
此时登录的时候需要以windows身份验证,否则会拒绝访问。
右键数据库文件夹,点击附加
新窗口中点击“添加”按钮,浏览到build.mdf文件。
入门 使用
现在数据表login_user中有三行数据,我们将其读入到一个comboBox中。
C# 数据库框架类
public class Database { public SqlConnection getConnection() { return new SqlConnection(@"server=localhost\doesthismatter;uid=sa;pwd=123456;database=build"); } SqlConnection con = null; /// <summary> /// 执行SQL语句 /// </summary> /// <param name="sql">SQL语句</param> /// <returns>返回一个具体值</returns> public object QueryScalar(string sql) { Open();//打开数据连接 object result = null; try { using(SqlCommand cmd=new SqlCommand(sql,con)) { result = cmd.ExecuteScalar(); return result; } } catch { return null; } } /// <summary> /// 执行SQL语句 /// </summary> /// <param name="sql">要执行的SQL语句</param> /// <param name="prams">参数</param> /// <returns></returns> public object QueryScalar(string sql,SqlParameter[]prams) { Open(); object result = null; try { using (SqlCommand cmd = CreateCommandSql(sql, prams)) { result = cmd.ExecuteScalar(); return result; } } catch { return null; } } /// <summary> /// 创建一个Sqlcommand对象,用来构建SQL语句 /// </summary> /// <param name="sql">sql语句</param> /// <param name="prams">sql所需要的参数</param> /// <returns></returns> public SqlCommand CreateCommandSql(string sql, SqlParameter[] prams) { Open(); SqlCommand cmd = new SqlCommand(sql,con); if (prams != null) { foreach (SqlParameter parameter in prams) { cmd.Parameters.Add(parameter); } } return cmd; } private void Open() { if (con == null) { //con = new SqlConnection("server=.;uid=home;pwd=;database=build"); con=new SqlConnection(@"server=localhost\doesthismatter;uid=sa;pwd=123456;database=build"); } if (con.State == ConnectionState.Closed) { con.Open(); } } /// <summary> /// 要执行SQL语句,该方法返回一个DataTable /// </summary> /// <param name="sql">执行SQL语句</param> /// <returns></returns> public DataTable Query(string sql) { Open(); using (SqlDataAdapter sqlda = new SqlDataAdapter(sql, con)) { using (DataTable dt = new DataTable()) { sqlda.Fill(dt); return dt; } } } /// <summary> /// 执行SQL语句,返回DataTable /// </summary> /// <param name="sql">要执行的SQL语句</param> /// <param name="prams">构建SQL语句所需要的参数</param> /// <returns></returns> public DataTable Query(string sql,SqlParameter[]prams) { SqlCommand cmd = CreateCommandSql(sql,prams); using (SqlDataAdapter sqldata = new SqlDataAdapter(cmd)) { using (DataTable dt = new DataTable()) { sqldata.Fill(dt); return dt; } } } /// <summary> /// 执行SQL语句,返回影响的记录行数 /// </summary> /// <param name="sql">要执行的SQL语句</param> /// <returns></returns> public int RunSql(string sql) { int result = -1; try { Open(); using (SqlCommand cmd = new SqlCommand(sql, con)) { result= cmd.ExecuteNonQuery(); con.Close(); return result; } } catch { return 0; } } /// <summary> /// 执行SQL语句,返回影响的记录行数 /// </summary> /// <param name="sql">要执行的SQL语句</param> /// <param name="prams">SQL语句所需要的参数</param> /// <returns></returns> public int RunSql(string sql,SqlParameter[]prams) { try { int result = -1; SqlCommand cmd = CreateCommandSql(sql, prams); result = cmd.ExecuteNonQuery(); this.Close(); return result; } catch { return 0; } } public void Close() { if (con != null) con.Close(); } }
窗口初始化的加载代码
private void login_Load(object sender, EventArgs e) { this.skinEngine1.SkinFile = "DiamondBlue.ssk"; try { string sql = "select * from login_user"; DataTable dd = data.Query(sql); this.comboBox1.DataSource = dd; this.comboBox1.DisplayMember = "_name"; } catch { } finally { data.Close(); } }
效果