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

时间:2011-11-29 23:07:40

标签: c#

我一直收到这个错误: 初始化字符串的格式不符合从索引0开始的规范。

这行代码:

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

我认为我需要sql语句而不是Ole,我不确定。

这是我的表格html代码:

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

这是我的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)
    {

    }
}

2 个答案:

答案 0 :(得分:1)

OleDbConnection采用实际的连接字符串,而不是连接字符串的NAME。您必须使用ConfigurationManager.ConnectionStrings["PayrollSystem_DBConnectionString"].ConnectionString从配置中获取连接字符串,并将其传递给OleDbConnection

答案 1 :(得分:0)

此外,如果您使用的是64位系统,则需要更改连接字符串以使用新的提供程序Microsoft.ACE.OLEDB.14.0

您可以在此处下载: