C#连接SQL Server数据库

下面是我用的获得连接的代码片段,主要是连接字符串的创建。是Windows连接(老师检查。。不用Sql Server用户名及密码连接)其中Data Source = 数据库实例。我的数据库实例是 SQLEXPRESS 。安装SQL Server时,默认就这个。Initial Catalog=数据库名(我的物理文件名和逻辑数据库名一样,不知道有没有这两种名的说法)

-- 下下周检查数据库设计,若还有人问我,我就给其看这篇文章(写得很粗糙),我在QQ中描述不清楚,怕看不懂同学甩的一张异常截图

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyBlog.DAL
{
    class DBUtils
    {
        public static SqlConnection GetConnection()
        {
            String connectstring = "Data Source=.\\SQLEXPRESS;" 
                + "Initial Catalog=myblog;"
                + "Integrated Security=True";
            SqlConnection sqlConn = new SqlConnection(connectstring);
            return sqlConn;
        }
    }
}


下面是我的用户表对应的部分数据库访问操作。可以借鉴修改成自己的。这都是数据访问层的,其他层按照老师课件来

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyBlog.Models;
using MyBlog.DAL;

namespace MyBlog.DAL
{
    public class UserService
    {
        public static Account GetUserByID(string Id)
        {
            SqlConnection sqlConn = DBUtils.GetConnection();
            sqlConn.Open();
            String sql = "select * from account where aid = " + Id;
            SqlCommand myCmd = new SqlCommand(sql, sqlConn);
            SqlDataReader myReader = myCmd.ExecuteReader();
            Account account = new Account();
            if (myReader.Read())
            {
                account.Aid = System.Convert.ToInt32(myReader["aid"]);
                account.Aname = (string)myReader["aname"];
                account.Password = (string)myReader["password"];
                account.Email = (string)myReader["email"];
                account.Gender = (string)myReader["gender"];

            }
            myReader.Close();
            sqlConn.Close();
            return account;
        }
        public static Account GetUserByName(string Aname)
        {
            SqlConnection sqlConn = DBUtils.GetConnection();
            //if (sqlConn.AccessToken == true)
            sqlConn.Open();  
            String sql = "select * from account where aname = '" + Aname + "'";
            SqlCommand myCmd = new SqlCommand(sql, sqlConn);
            SqlDataReader myReader = myCmd.ExecuteReader();
            Account account = new Account();
            if (myReader.Read())
            {
                account.Aid = System.Convert.ToInt32(myReader["aid"]);
                account.Aname = (string)myReader["aname"];
                account.Password = (string)myReader["password"];
                account.Email = (string)myReader["email"];
                account.Gender = (string)myReader["gender"];
                account.Phone = (string)myReader["phone"];
                account.Moto = (string)myReader["moto"];
                account.School = (string)myReader["school"];
            }
            myReader.Close();
            sqlConn.Close();
            return account;
        }
        public static bool Insert(string nickname, string password, string email, string gender, string school,
            string phone, string moto)
        {
            SqlConnection sqlConn = DBUtils.GetConnection();
            try
            {
                sqlConn.Open();
                string sql = "insert into account (aname, password, email, gender, school, phone, moto, time)" +
                "values('" + nickname + "', '" + password + "', '" + email + "', '" + gender + "'," +
                " '" + school + "', '" + phone + "', '" + moto + "', GETDATE())";
            SqlCommand myCmd = new SqlCommand(sql, sqlConn);
                myCmd.ExecuteNonQuery();
                return true;
            } catch (Exception)
            {
                return false;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/baisedeqingting/article/details/80290138