zoukankan      html  css  js  c++  java
  • ADO.NET Entity Framework 数据库连接字符串手工设置

    一般情况,可以直接使用系统自动配置的连接字符串。如果要发布或者多个数据源,则

    需要手工设置数据库连接。  示例如下:

     // Specify the provider name, server and database.

                string providerName = "System.Data.SqlClient";
                string serverName = "DBServer";
                string databaseName = "databaseName";

                // Initialize the connection string builder for the
                // underlying provider.
                SqlConnectionStringBuilder sqlBuilder =
                    new SqlConnectionStringBuilder();

                // Set the properties for the data source.
                sqlBuilder.DataSource = serverName;
                sqlBuilder.InitialCatalog = databaseName;
                sqlBuilder.IntegratedSecurity = false;
                sqlBuilder.UserID = "sa";
                sqlBuilder.Password = "sa";

                // Build the SqlConnection connection string.
                string providerString = sqlBuilder.ToString();

                // Initialize the EntityConnectionStringBuilder.
                EntityConnectionStringBuilder entityBuilder =
                    new EntityConnectionStringBuilder();

                //Set the provider name.
                entityBuilder.Provider = providerName;

                // Set the provider-specific connection string.

                entityBuilder.ProviderConnectionString = providerString;

                entityBuilder.Provider = providerName;


                // Set the Metadata location.
                entityBuilder.Metadata = "res://*" ; //或从.config文件中copy
                                //@"res://*/NBModel.csdl|
                                //res://*/NBModel.ssdl|
                                //res://*/NBModel.msl";
                string str = entityBuilder.ToString();

                using (NORTHWNDEntities nwt = new NORTHWNDEntities(entityBuilder.ToString()))
                {
                    var query = from cat in nwt.Categories
                                select cat;
                    this.GridView1.DataSource = query;
                    this.GridView1.DataBind();

                }
  • 相关阅读:
    Python 爬虫js加密破解(一) 爬取今日头条as cp 算法 解密
    Python 爬虫实例(2)—— 爬取今日头条
    Python 爬虫实例(1)—— 爬取百度图片
    python 操作redis之——HyperLogLog (八)
    python 操作redis之——有序集合(sorted set) (七)
    Python操作redis系列之 列表(list) (五)
    Python操作redis系列以 哈希(Hash)命令详解(四)
    Python操作redis字符串(String)详解 (三)
    How to Install MySQL on CentOS 7
    Linux SSH远程文件/目录 传输
  • 原文地址:https://www.cnblogs.com/simonhaninmelbourne/p/1413658.html
Copyright © 2011-2022 走看看