简体   繁体   English

我正在尝试通过web.config文件建立与数据库的连接-只是不起作用

[英]I'm trying to set up a connection to my database via the web.config file - It's just not working

I'm developing an application with C#/ASP.NET and it needs to connect to my SQL database. 我正在使用C#/ ASP.NET开发应用程序,它需要连接到我的SQL数据库。 I have everything set up, but I'm having a really hard time getting the web.config to work. 我已经完成所有设置,但是要让web.config正常工作非常困难。 What I'm trying to do is set up the connection string to later reference it on my code. 我想做的是设置连接字符串,以便以后在我的代码中引用它。

Here's my code: 这是我的代码:

  <add name="BancoMaravilhoso" providerName="System.Data.SqlClient" connectionString="server=myserver;database=beirao_teste;uid=sa;password=123123;" />

  <add name="ApplicationServices"
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
     providerName="System.Data.SqlClient" />

When I run the build it loads the page just fine. 当我运行构建时,它可以正常加载页面。 I made a function to call a stored procedure from my database on the pageload, but it's not being called. 我做了一个函数,可以从页面加载时从数据库中调用存储过程,但是没有被调用。 Also, I noticed that if I change the stuff within the connection string tab, like password, it doesn't throw me any errors. 另外,我注意到,如果我更改了连接字符串选项卡中的内容(例如密码),它不会引发任何错误。

OBS. OBS。 I'm new to ASP.NET and whatnot 我是ASP.NET的新手

Thanks in advance! 提前致谢!

EDIT: 编辑:

The call to my stored procedure: 对我的存储过程的调用:

public class Pessoa : Base
    {
        public void CriaPessoa(string Nome, string Sexo, string Data, string Email)
        {
        using (DbConnection cn = this.CreateConnection())
        {
            using (DbCommand cmd = this.CreateCommand(cn))
            {
                cmd.CommandText = "CADASTRAR";
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(this.CreateParameter("@Nome", DbType.String, Nome));
                cmd.Parameters.Add(this.CreateParameter("@Sexo", DbType.String, Sexo));
                cmd.Parameters.Add(this.CreateParameter("@Data", DbType.String, Data));
                cmd.Parameters.Add(this.CreateParameter("@Email", DbType.String, Email));
            }
        }
    }
}

AND THEN: 接着:

protected void Page_Load(object sender, EventArgs e)
    {
        Pessoa p1 = new Pessoa();

        p1.CriaPessoa("Fulano", "M", "27/10/2012", "fulano@ciclano.com");

    }

Please see my example. 请看我的例子。

Web.config: Web.config:

<connectionStrings>
    <add name="DBC" connectionString="data source=.; database=DB; 
         integrated security=SSPI" providerName="System.Data.SqlClient"/>
</connectionStrings>

Try this in the Web Form: 在Web表单中尝试以下操作:

string CN = ConfigurationManager.ConnectionStrings["DBC"].ConnectionString;
using (DbConnection cn = this.CreateConnection())
{
    using (DbCommand cmd = this.CreateCommand(CN))
    {
        cmd.CommandText = "CADASTRAR";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(this.CreateParameter("@Nome", DbType.String, Nome));
        cmd.Parameters.Add(this.CreateParameter("@Sexo", DbType.String, Sexo));
        cmd.Parameters.Add(this.CreateParameter("@Data", DbType.String, Data));
        cmd.Parameters.Add(this.CreateParameter("@Email", DbType.String, Email));
    }
}

in solution explorer, right click on your project and click on properties. 在解决方案资源管理器中,右键单击您的项目,然后单击属性。 in right hand you'll see some option.click on settings and create one new setting ,select your setting type as connection string, in 'value' column a button appears,click it and enter your connection properties in 'connection properties' window, then click on 'testconnection' and ok.created connection string is correct connection string and will add to that project's webconfig automatically 在右侧,您会看到一些选项。单击设置并创建一个新设置,选择设置类型作为连接字符串,在“值”列中显示一个按钮,单击它,然后在“连接属性”窗口中输入连接属性,然后单击“ testconnection”,确定创建的连接字符串是正确的连接字符串,并将自动添加到该项目的webconfig中

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

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