简体   繁体   English

连接数据库ASP.NET C#时出错

[英]Error connecting to database ASP.NET C#

I keep getting this error: Format of the initialization string does not conform to specification starting at index 0. 我不断收到此错误:初始化字符串的格式不符合从索引0开始的规范。

This line of code: 这行代码:

using (OleDbConnection conn = new OleDbConnection("PayrollSystem_DBConnectionString"))

I think I need sql statements instead of Ole, I'm not sure. 我不确定我需要sql语句而不是Ole,我不确定。

Here is my form html code: 这是我的表单html代码:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:PayrollSystem_DBConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:PayrollSystem_DBConnectionString.ProviderName %>"

Here is my frmManageUsers code: 这是我的frmManageUsers代码:

public partial class frmManageUsers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnAddUser_Click1(object sender, EventArgs e)
    {
                //string userName, userPassword;



        if (txtUserName.Text == "" || txtUserName.Text == null)
        {
            lblError.Text = ("User Name may not be empty");
            lblError.ForeColor = System.Drawing.Color.Red;
            return;
        }
       // else

           // userName = (txtUserName.Text);


        if (txtPassword.Text == "" || txtPassword.Text == null)
        {
            lblError.Text = ("Password may not be empty");
            lblError.ForeColor = System.Drawing.Color.Red;
            return;
        }
        //else
           // userPassword = (txtPassword.Text);

        using (OleDbConnection conn = new OleDbConnection("PayrollSystem_DBConnectionString"))
            {
                string insert = "Insert INTO tblUserLogin (UserName, UserPassword, SecurityLevel) Values (@UserName, @UserPassword, @SecurityLevel)";
                OleDbCommand cmd = new OleDbCommand(insert, conn);
                cmd.Parameters.Add("@UserName", txtUserName.Text);
                cmd.Parameters.Add("@UserPassword", txtPassword.Text);
                cmd.Parameters.Add("@SecurityLevel", drpdwnlstSecurityLevel.SelectedValue);  
                cmd.ExecuteNonQuery();      
            }

        Session["UserName"] = txtUserName.Text;
        Session["Password"] = txtPassword.Text;
        Session["SecurityLevel"] = drpdwnlstSecurityLevel.SelectedValue;
        Server.Transfer("frmManageUsers.aspx");

        //Server.Transfer("grdUserLogin"); 

    }
    protected void drpdwnlstSecurityLevel_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

OleDbConnection takes the actual connection string, not the NAME of the connection string. OleDbConnection采用实际的连接字符串,而不是连接字符串的名称。 You have to get the connection string from the Configuration using ConfigurationManager.ConnectionStrings["PayrollSystem_DBConnectionString"].ConnectionString and pass that to OleDbConnection 您必须使用ConfigurationManager.ConnectionStrings["PayrollSystem_DBConnectionString"].ConnectionString从配置中获取连接字符串,并将其传递给OleDbConnection

Also, if you're using a 64-bit system, you need to change the connection string to use the new provider, Microsoft.ACE.OLEDB.14.0 另外,如果您使用的是64位系统,则需要更改连接字符串以使用新的提供程序Microsoft.ACE.OLEDB.14.0

You can download it here: 您可以在这里下载:

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

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