代码改变世界

app.config读写

2008-09-24 12:16  Iron  阅读(273)  评论(0编辑  收藏  举报

Posts 55
Re: Get connection string from app.config file
Comment This post has a code sample within it. Was this post helpful ?
Reply Quote
It's still not working for me... CondigurationManager is not even recognized as a class although i have added

using

System.Configuration; line

I get "ConfigurationManager does not exist in the current context message"

why is that ?

this is the code i want to work:

using System;

using System.Collections.Generic;

using System.Text;

using System.Configuration;

using System.Data.SqlClient;

using System.IO;

 

namespace myMoney

{

 

    class DB

    {

        const string connectionSting = ConfigurationManager.ConnectionStrings["myMoney.Properties.Settings.myMoneyConnectionString"].ConnectionString;

       

        public static string GetSingleValue(string queryString)

        {

            object queryResult;

 

            using (SqlConnection connection = new SqlConnection(

                       connectionSting))

            {

                SqlCommand command = new SqlCommand(queryString);

                command.Connection = connection;

                connection.Open();

 

                queryResult = command.ExecuteScalar();

                connection.Close();

                return queryResult.ToString();

            }

        }

 

        public static void WriteSingleValue(string updateString)

        {

            using (SqlConnection connection = new SqlConnection(connectionSting))

            {

                SqlCommand command = new SqlCommand(updateString);

                command.Connection = connection;

                connection.Open();

                command.ExecuteNonQuery();

                connection.Close();

            }

        }

    }

}