C# 链接SQLServer 图解

本文介绍C#链接SQLServer数据库

Windows 7、VS 2008、SQLServer Manager Express

启动SQLServer Manager Express,选择win身份验证,填写一个服务器名称。点击——链接

C# 链接SQLServer 图解_第1张图片

 

打开本地数据库

C# 链接SQLServer 图解_第2张图片


数据库链接代码

Web中配置链接字符串:

<configuration>
  <appSettings>
    <add key="win"  value="Data Source=.\SQLEXPRESS;Initial Catalog=F:\仓库管理系统\database\net05jinxiaocun_Data.MDF;Integrated Security=True" />
  </appSettings>
  <connectionStrings>
 

创建SQLHelper类,获取SQLCOnnection对象

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public class SQLConnHelper {
   
    public static string connStr = ConfigurationSettings.AppSettings["win"];//读取WebConfig中的标签
    static SqlConnection mConn;
    public static SqlConnection getInstence() {
        if (mConn == null) {
            mConn = new SqlConnection(connStr);
        }
        
        return mConn;
    }
    
}

创建DBHelper类,数据操作类,一个查询方法和一个插入方法。

class DBHelper{
public static SqlCommand dbCommend(SqlConnection conn, string sql)
    {
        SqlCommand comm = new SqlCommand();
        comm.Connection = conn;
        comm.CommandType = CommandType.Text;
        comm.CommandText = sql;
        return comm;
    }
public static Boolean addSellHistory(SqlConnection conn, SellHistory f)
    {

        try
        {
            conn.Open();
            string sql = "insert into xiaoshou(name,count,pricein,provider,priceout,descripetion)values('" + f.name + "'," + f.count + ",'" + f.pricein + "','" + f.provider + "','" + f.priceout + "','" + f.descripetion + "')";
            //string sql = "insert into xiaoshou(count)values(234)";
            DBHelper.dbCommend(conn, sql).ExecuteReader();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            conn.Close();
        }
    
    }



public static State signIn(SqlConnection conn, string name, string pwd)
    {
        State state = new State();
        try
        {
            conn.Open();
            string sql = "select * from allusers where username='" + name + "' and pwd='" + pwd + "'";

            SqlDataReader dr = DBHelper.dbCommend(conn, sql).ExecuteReader();
            
            if (dr.Read())
            {
                state.STATE = State.SUCCESS;
                //保存用户状态
                AllUser user = new AllUser();
                user.id = Convert.ToInt32(dr[AllUser._ID]);
                user.name = Convert.ToString(dr[AllUser._USERNAME]);
                user.password = Convert.ToString(dr[AllUser._PWD]);
                user.cx = Convert.ToString(dr[AllUser._CX]);
                user.addtime = Convert.ToString(dr[AllUser._ADDTIME]);
                state.mUser = user;

            }
            else
            {

                state.STATE = State.ERROR;
                

            }
        }
        catch { }
        finally
        {
            conn.Close();
        }
        Console.WriteLine("success="+state.STATE);
        return state;


    }
}
}



你可能感兴趣的:(C#链接SQLServer)