正在连接到ASP.Net中的自定义数据库

本文关键字:自定义 数据库 Net ASP 连接 | 更新日期: 2023-09-27 18:26:26

我正在尝试创建一个小型应用程序,用户可以在其中提交个人信息,数据保存在Access DB上。

返回的错误没有任何意义,因为我试图在表中插入正确数量的值。

这些值包括第一、最后、年龄和性别。

这是我的代码,后面是我按下提交按钮时收到的错误。

谢谢你的帮助。

<pre>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
public void btnSubmit_Click(object sender, EventArgs e)
{
    // Create and configure connection string.
    OleDbConnection c = new OleDbConnection();
    c.ConnectionString = 
        "Provider=Microsoft.ACE.OLEDB.12.0;" +
        "Data Source=d:/ectserver/gnicolai/Database/application.accdb;";
    // Open connection.
    c.Open();
    // Get data from textboxes.
    string last = txtLastName.Text;
    string first = txtFirstName.Text;
    string gender = txtGender.Text;
    int  age = int.Parse(txtAge.Text);
    // Compose SQL command string.
    string sql = "INSERT INTO Applicant VALUES"  + 
        "('" + last + "', '" + first + 
        "', '" + gender + "',"  + age + ");";
    // Show SQL insert statement.
    litMessage.Text = 
        "Data submitted with SQL query string<br />" + sql;
    // Create command object and execute insert statement.
    OleDbCommand command = new OleDbCommand(sql, c);
    command.ExecuteNonQuery();
    // Close connection.
    c.Close();
}
</script>
<html>
<head>
<title>grocery-display3.aspx Example</title>
<link rel="stylesheet" class="text/css" 
                       href="../examples.css" />
<style type="text/css">
td  { padding-left:0.15cm;
      padding-right:0.15cm;
      padding-top:0.15cm;
      padding-bottom:0.15cm; }
</style>
</head>
<body>
<h2>PersonInfo3 Example</h2>
<p>Submit age, gender, and age for a person; 
     store this information in a database.
     Retrieve this data with the ViewPersons page. </p>
<form id="frmSelect" runat="server">
<table>
<tr> 
    <td class="r">Last Name</td>
    <td><asp:TextBox ID="txtFirstName" runat="server" 
            CssClass="ctrl" /></td>
</tr>
<tr> 
    <td class="r">First Name</td>
    <td><asp:TextBox ID="txtLastName" runat="server" 
            CssClass="ctrl" /></td>
</tr>
<tr> 
    <td class="r">Gender</td>
    <td><asp:TextBox ID="txtGender" runat="server"
            CssClass="ctrl"  /></td>
</tr>
<tr> 
    <td class="r">Age</td>
    <td><asp:TextBox ID="txtAge" runat="server" 
            CssClass="ctrl" /></td>    
</tr>
<tr> 
    <td> </td>
    <td><asp:Button ID="btnSubmit" runat="server"
            Text="Submit" CssClass="ctrl" Width="128px"
            OnClick="btnSubmit_Click" /></td>
</tr>
</table>
<p><asp:RangeValidator ID="RangeValidator1" Type="Integer" runat="server" 
      ControlToValidate="txtAge" Display="Static" MinimumValue="0" 
      MaximumValue="130" ErrorMessage="Age must be between 0 and 130" /></p>
<p><asp:Literal ID="litMessage" runat="server" /></p>
</form>
</body>
</html>         
</pre>

<strong>Error</strong>
<pre>
Server Error in '/' Application.
Number of query values and destination fields are not the same.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.Data.OleDb.OleDbException: Number of query values and destination fields are not the same.
Source Error: 

Line 32:     // Create command object and execute insert statement.
Line 33:     OleDbCommand command = new OleDbCommand(sql, c);
Line 34:     command.ExecuteNonQuery();
Line 35:         
Line 36:     // Close connection.
Source File: d:'DePaul'Winter 2012'IT 330'Projects'Proj5-Nicolaides'Proj5-Nicolaides'Default.aspx    Line: 34 
Stack Trace: 

[OleDbException (0x80004005): Number of query values and destination fields are not the same.]
   System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) +992124
   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.default_aspx.btnSubmit_Click(Object sender, EventArgs e) in d:'DePaul'Winter 2012'IT 330'Projects'Proj5-Nicolaides'Proj5-Nicolaides'Default.aspx:34
   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

</pre>

正在连接到ASP.Net中的自定义数据库

最有可能的问题是您的表定义有4列以上。您需要为额外的列提供一个默认的插入值(如果数据库支持的话),或者更改插入代码以匹配。

同样值得指出的是,此代码会受到sql注入攻击。您永远不应该构建这样的sql语句并将其直接提供给数据库。相反,将其转换为sql参数,然后发送。

您可能需要明确指定要在表中填充的列。您要插入的列的名称是什么?

尝试将您的代码更改为:

string sql = "INSERT INTO Applicant" +
    "(LastName, FirstName, Gender, Age)" +
    "VALUES"  + 
    "('" + last + "', '" + first + 
    "', '" + gender + "',"  + age + ");";

将LastName等列名替换为您的列名。

数据库表上的ID字段扰乱了插入语句。在设计视图中查看该表后,我删除了"ID"字段,插入语句正常工作。