zoukankan      html  css  js  c++  java
  • C#基础——数据库连接字符串及数据库访问

    SQL Server Express数据库连接字符串: 

    con.ConnectionString =@"Data Source=localhostSQLEXPRESS;Initial Catalog=databasename;Integrated Security=false;uid=sa;

    pwd=password"

    Initial Catalog:数据库名称。

    Integrated Security:false表示使用SQL Server身份认证(sa,password),true表示使用Windows身份认证。 

    示例: 

    需要加一个Label控件 

    完整实验代码:

    protected void Page_Load(object sender, EventArgs e)
            {
                SqlConnection con=new SqlConnection();
                con.ConnectionString =@"Data Source=localhostSQLEXPRESS;Initial Catalog=databasename;Integrated Security=false;uid=sa;pwd=password"
                try{
                    con.Open();
                    Label1.Text="success";
                    con.Close();
                }
                catch{
                    Label1.Text="false";
                }                                                                                                                                   }

    C#数据库访问需要添加的民命空间:

    访问sql server需要使用命名空间:using System.Data.SqlClient;

    访问Access需要使用命名空间:using System.Data.OleDb;

  • 相关阅读:
    NOIP2015 斗地主
    BZOJ 2120: 数颜色
    BZOJ 1014: [JSOI2008]火星人prefix
    BZOJ 4665: 小w的喜糖
    BZOJ 3665: maths
    BZOJ 3270: 博物馆
    BZOJ 1419: Red is good
    【转】二分图的最大匹配
    POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)
    POJ 2485 Highway(Prim+邻接矩阵)
  • 原文地址:https://www.cnblogs.com/bluewhy/p/5050652.html
Copyright © 2011-2022 走看看