当前位置: 首页 > 知识库问答 >
问题:

从ASP/C#Web表单在Access数据库中插入数据

殳宸
2023-03-14

我写的ASP/C#页面,我必须采取我已经创建的形式,并将其插入到MS访问数据库。使用以下字段设置MS Access数据库:

ID,用户名,密码,名字,姓,地址,地址2,城市,州,邮政编码,电子邮件,性别,年龄,衬衫尺寸,裤子尺寸,电子邮件订阅。

我得到以下错误:

异常详细信息:System.Data.OleDb.OLEDBEException:未为一个或多个必需参数提供值。

我无法找出哪个参数缺少值。以下是堆栈跟踪:

[  OleDbException (0x80040e10): No value given for one or more required parameters.]
   System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) +992156
   System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +255
   System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +188
   System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +58
   System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +161
   System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +113
   ASP.ado_aspx.SubmitForm(Object sender, EventArgs e) in e:\ectserver\ADELEO10\ado.aspx:165
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

下面是页面后面的代码:

                public void SubmitForm(object sender, EventArgs e)
                {
                    if(Page.IsValid)
                    {
                        string provider = ConfigurationManager.ConnectionStrings["databaseConnString"].ProviderName; 

                        DbProviderFactory factory = DbProviderFactories.GetFactory(provider);

                       //Open Connection
                        DbConnection conn = factory.CreateConnection();

                        //Assign Connection String
                        conn.ConnectionString = ConfigurationManager.ConnectionStrings["databaseConnString"].ConnectionString; 
                        //Connection Open
                        conn.Open();

                        //Initialize Command
                        DbCommand comm = conn.CreateCommand();
                        //Tell command which connection it will use
                        comm.Connection = conn;
                        //Give command SQL to execute
                        comm.CommandText ="Insert into userinfo(UserName, Passwrd, FirstName, LastName, Address, Address2, City, State, ZipCode, Email, Gender, Age, ShirtSize, PantSize, EmailSubscribe) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";                     

                        DbParameter param;

                        param = comm.CreateParameter();
                        param.DbType = DbType.String;
                        param.Direction = ParameterDirection.Input;
                        param.Value = userName.Text;

                        comm.Parameters.Add(param);                     

                        param = comm.CreateParameter();
                        param.DbType = DbType.String;
                        param.Direction = ParameterDirection.Input;
                        param.Value = password.Text;

                        comm.Parameters.Add(param); 

                        param = comm.CreateParameter();
                        param.DbType = DbType.String;
                        param.Direction = ParameterDirection.Input;
                        param.Value = firstName.Text;

                        comm.Parameters.Add(param);                                 

                        param = comm.CreateParameter();
                        param.DbType = DbType.String;
                        param.Direction = ParameterDirection.Input;
                        param.Value = lastName.Text;

                        comm.Parameters.Add(param);

                        param = comm.CreateParameter();
                        param.DbType = DbType.String;
                        param.Direction = ParameterDirection.Input;
                        param.Value = address.Text;

                        comm.Parameters.Add(param); 

                        param = comm.CreateParameter();
                        param.DbType = DbType.String;
                        param.Direction = ParameterDirection.Input;
                        param.Value = address2.Text;

                        comm.Parameters.Add(param); 

                        param = comm.CreateParameter();
                        param.DbType = DbType.String;
                        param.Direction = ParameterDirection.Input;
                        param.Value = city.Text;

                        comm.Parameters.Add(param);

                        param = comm.CreateParameter();
                        param.DbType = DbType.String;
                        param.Direction = ParameterDirection.Input;
                        param.Value = state.Text;

                        comm.Parameters.Add(param);

                        param = comm.CreateParameter();
                        param.DbType = DbType.Int32;
                        param.Direction = ParameterDirection.Input;
                        param.Value = Int32.Parse(zipCode.Text);

                        comm.Parameters.Add(param);

                        param = comm.CreateParameter();
                        param.DbType = DbType.String;
                        param.Direction = ParameterDirection.Input;
                        param.Value = email.Text;

                        comm.Parameters.Add(param);

                        param = comm.CreateParameter();
                        param.DbType = DbType.String;
                        param.Direction = ParameterDirection.Input;
                        param.Value = GenderRadioGroup.Text;

                        comm.Parameters.Add(param);

                        param = comm.CreateParameter();
                        param.DbType = DbType.Int32;
                        param.Direction = ParameterDirection.Input;
                        param.Value = Int32.Parse(age.Text);

                        comm.Parameters.Add(param);

                        param = comm.CreateParameter();
                        param.DbType = DbType.String;
                        param.Direction = ParameterDirection.Input;
                        param.Value = drpShirtSizeCategory.SelectedValue;

                        comm.Parameters.Add(param);

                        param = comm.CreateParameter();
                        param.DbType = DbType.String;
                        param.Direction = ParameterDirection.Input;
                        param.Value = drpPantSizeCategory.SelectedValue;

                        comm.Parameters.Add(param);

                        param = comm.CreateParameter();
                        param.DbType = DbType.String;
                        param.Direction = ParameterDirection.Input;
                        param.Value = EmailRadioGroup.Text;

                        comm.Parameters.Add(param);

                        //Execute command get back result via reader
                        int totalCount = comm.ExecuteNonQuery();

                        DbCommand comm2 = conn.CreateCommand();
                        comm2.CommandText = "Select @@Identity";
                        comm2.Connection = conn;

                        String id = comm2.ExecuteScalar().ToString();
                        conn.Close();

                        lblMessage.Text = "ID of User Added = " +id;
                    }
                }

感谢您的帮助。谢谢

共有2个答案

柳业
2023-03-14

为了确保您具有正确的标识,最好将IDENT_调用为CURRENT('TableName')

DbCommand comm2 = conn.CreateCommand();
comm2.CommandText = "Select IDENT_CURRENT('userinfo')";
comm2.Connection = conn;

String id = comm2.ExecuteScalar().ToString();
conn.Close();

请看这里的解释。

充阳秋
2023-03-14

试试这个

string provider = ConfigurationManager.ConnectionStrings["databaseConnString"].ProviderName; 

        DbProviderFactory factory = DbProviderFactories.GetFactory(provider);

       //Open Connection
        DbConnection conn = factory.CreateConnection();

        //Assign Connection String
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["databaseConnString"].ConnectionString; 
        //Connection Open
        conn.Open();

        //Initialize Command
        DbCommand comm = conn.CreateCommand();
        //Tell command which connection it will use
        comm.Connection = conn;
        //Give command SQL to execute
        comm.CommandText ="Insert into userinfo(UserName, Passwrd, FirstName, LastName, Address, Address2, City, State, ZipCode, Email, Gender, Age, ShirtSize, PantSize, EmailSubscribe) values(@UserName, @Passwrd, @FirstName, @LastName, @Address, @Address2, @City, @State, @ZipCode, @Email, @Gender, @Age, @ShirtSize, @PantSize, @EmailSubscribe)";                     


        SqlParameter userName = new SqlParameter("@UserName", SqlDbType.VarChar);
        userName .Value = userName.Text;
        comm.Parameters.Add(userName);

        SqlParameter password = new SqlParameter("@Passwrd", SqlDbType.VarChar);
        password .Value = password.Text;
        comm.Parameters.Add(password);

        SqlParameter firstName = new SqlParameter("@FirstName", SqlDbType.VarChar);
        firstName .Value = FirstName.Text;
        comm.Parameters.Add(firstName);

        SqlParameter lastName = new SqlParameter("@LastName", SqlDbType.VarChar);
        lastName .Value = lastName.Text;
        comm.Parameters.Add(lastName);


        SqlParameter address = new SqlParameter("@Address", SqlDbType.VarChar);
        address.Value = address.Text;
        comm.Parameters.Add(address);

        SqlParameter address2 = new SqlParameter("@Address2", SqlDbType.VarChar);
        address2.Value = address2.Text;
        comm.Parameters.Add(address2); 

        SqlParameter city = new SqlParameter("@City", SqlDbType.VarChar);
        city.Value = city.Text;
        comm.Parameters.Add(city); 

        SqlParameter state = new SqlParameter("@State", SqlDbType.VarChar);
        state.Value = state.Text;
        comm.Parameters.Add(state); 

        SqlParameter zipCode = new SqlParameter("@ZipCode", SqlDbType.VarChar);
        zipCode.Value = zipCode.Text;
        comm.Parameters.Add(zipCode);

        SqlParameter email = new SqlParameter("@Email", SqlDbType.VarChar);
        email.Value = email.Text;
        comm.Parameters.Add(email);

        SqlParameter genderRadioGroup = new SqlParameter("@GenderRadioGroup", SqlDbType.VarChar);
        genderRadioGroup.Value = genderRadioGroup.Text;
        comm.Parameters.Add(genderRadioGroup);

        SqlParameter age = new SqlParameter("@Age", SqlDbType.VarChar);
        age.Value = age.Text;
        comm.Parameters.Add(age);

        SqlParameter drpShirtSizeCategory = new SqlParameter("@ShirtSize", SqlDbType.VarChar);
        drpShirtSizeCategory.Value = drpShirtSizeCategory.Text;
        comm.Parameters.Add(drpShirtSizeCategory);

        SqlParameter drpPantSizeCategory = new SqlParameter("@PantSize", SqlDbType.VarChar);
        drpPantSizeCategory.Value = drpPantSizeCategory.Text;
        comm.Parameters.Add(drpPantSizeCategory);

        SqlParameter emailRadioGroup = new SqlParameter("@EmailSubscribe", SqlDbType.VarChar);
        emailRadioGroup.Value = EmailRadioGroup.Text;
        comm.Parameters.Add(emailRadioGroup);



        //Execute command get back result via reader
        int totalCount = comm.ExecuteNonQuery();

        DbCommand comm2 = conn.CreateCommand();
        comm2.CommandText = "Select @@Identity";
        comm2.Connection = conn;

        String id = comm2.ExecuteScalar().ToString();
        conn.Close();

        lblMessage.Text = "ID of User Added = " +id;
 类似资料:
  • 请在Access数据库中插入数据时遇到错误代码。它一直说我的INSERT INTO语句中存在sytanx错误。有人能帮我解决这个问题吗?这是代码

  • 我已经建立了一个Access数据库,现在我正试图从SQL数据库将数据导入Access数据库表。Access数据库表现在是空的,我正试图从SQL数据库导入数据,只有一个键值。例如,我只想导入符合以下条件的数据。 我已经设置了到表的ODBC链接。我一直在寻找这样做的方法,但没有找到任何好的方法。 使现代化 找到了答案,对于任何需要知道如何做到这一点的人,以下是步骤。

  • 我忙于一项学校作业,我有一个由三个表组成的基本access数据库。Book、Reader和BookRated,其中as BookRated是交集表,主键userName来自Reader和ISBN来自Book组成BookRated的复合主键(作为外键)。 我正试图将信息插入书签表中,但出现以下错误

  • 问题内容: 我试图创建一个查询,将保留联接两个表(这些表在我的本地驱动器中的两个不同的数据库中)。到目前为止,我已经提出了这个SQL语句,但是当我运行查询时,它告诉我FROM子句中的语法有错误。 与本地表(Daily_Report)相比,查询输出一个新表(AUDIT_TABLE)并包含外部表(YTD-Daily_Report)中不存在的记录。 我从来没有在单独的数据库中查询表,所以我在这里很迷失,

  • 我正在尝试创建一个在线简历,并已将所有信息存储到Access DB中,当我使用ASP按钮创建标签,然后尝试使用标签在te DB中查找相关信息时,我需要以下错误的帮助。 下面是我的错误 FROM子句中存在语法错误。描述:在执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:系统。数据OleDb。OLEDBEException:FROM

  • 问题内容: 我最近开始使用访问权限。我试图在数据库中插入几行;但是,我被卡住了,因为它引发了一个错误: 参数太少。 我有一个表测试,其中只有一个名为start_date的列,我想在两个日期之间插入所有日期,例如,如果我考虑将2014年7月1日至2014年7月7日,我需要将日期设为1/7 / 2014,2 / 7 / 2014,3 / 7/2014在我的表格中,但是我在插入所用代码时遇到问题如下 我