如何解决connectionstring属性未初始化的问题 [英] How do I solve the problem of connectionstring property not initialized

查看:122
本文介绍了如何解决connectionstring属性未初始化的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图使用存储过程将数据传递到数据库



我尝试过:



我在web.config中声明了连接字符串

was trying to pass data to database using stored Procedure

What I have tried:

I have declared the connection string in my web.config

推荐答案

简单:初始化连接字符串...

只是把它放到你的web.config中除了提供一个保留它的位置之外什么都不做。您需要检索字符串并将其传递给您通过代码与数据库通信的SqlConnection,MySqlConnection或OledbConnection对象。

例如:

Simple: initialize the connection string...
Just putting it into your web.config doesn't do anything except provide a place to keep it. It's up to you to retrieve the string and pass it to the SqlConnection, MySqlConnection, or OledbConnection object that you communicate with the database via in your code.
For example:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Id, description FROM myTable", con))
        {
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["Id"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", id, desc);
                }
            }
        }
    }

将web.config连接字符串加载到strConnect的位置。

Where you load the web.config connection string into strConnect.


这篇关于如何解决connectionstring属性未初始化的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆