简体   繁体   English

空连接字符串

[英]Null Connectionstring

I write a solution in VS2010 that have some C# and asp.net project in it.我在 VS2010 中编写了一个解决方案,其中包含一些 C# 和 asp.net 项目。

I have a configuration class in C# project to make connectionstring for connect to database and it worked well in C# projects, But when I want to use it in Asp web forms the class return a null connectionstring!!!我在 C# 项目中有一个配置类来创建用于连接数据库的连接字符串,它在 C# 项目中运行良好,但是当我想在 Asp web 表单中使用它时,该类返回一个空连接字符串!!!

Please help me because I can't find anything about this problem in web.请帮助我,因为我在 web.xml 中找不到有关此问题的任何信息。

Is that need to write another class and using web.config to make connectionstring for asp projects??是否需要编写另一个类并使用 web.config 为 asp 项目创建连接字符串?? or I can reuse class of C# projects for asp projects??或者我可以将 C# 项目类重用于 asp 项目?

 protected void Button_Save_Click(object sender, EventArgs e)
    {
        try
        {
            this.intCreatedID = Aryana.Data.Contact.Create(int.Parse(Aryana.Data.FormPublicSetting.GetOption("DefaultGroup"));
        }

///The Method to load data from db(the connectionstring in this method return null ///从db加载数据的方法(该方法中的connectionstring返回null

 public static string GetOption(string title)
        {
            using (SqlConnection connection = new SqlConnection(Aryana.Data.Configuration.ConnectionString))
             {
                 using (SqlCommand command = new SqlCommand("SET NOCOUNT OFF SELECT Content FROM [dbo].[DefaultSetting] WHERE (Title = @Title)", connection))
                {
                    command.CommandType = CommandType.Text;

                command.Parameters.AddWithValue("@Title", title);

                connection.Open();

                using (SqlDataReader dr = command.ExecuteReader())
                {
                   if (dr.Read())
                    {
                        string value = (string)dr["Content"];
                        return value;
                    }
                    else
                        throw new ObjectDisposedException("title");
                }

            }
        }
    }

/// The method to make connectionstring /// 创建连接字符串的方法

 public static class Configuration
{
    static Configuration()
    {

        string ServerName = Properties.Settings.Default.Server;
        string DataBaseName = Properties.Settings.Default.DataBaseName;

connectionString = "data source=" + ServerName + "; initial catalog=" + DataBaseName + ";user id=sa;";

You can get connection string from web.config file您可以从 web.config 文件中获取连接字符串

web.config file: web.config 文件:

<configuration>
    <connectionStrings>
       <add name="ConnectionStringName" connectionString="connection string goes here"  />   
    </connectionStrings>
</configuration>

and you can then get the connection string in c# code with:然后您可以使用以下命令获取 c# 代码中的连接字符串:

string connString=ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;

ConfigurationManager class in is System.Configuration namespace. ConfigurationManager类是System.Configuration命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM