向基于另一个表的表插入数据 C#

发布于 2024-12-18 11:59:04 字数 1646 浏览 4 评论 0原文

我编写了一些代码,从一个表中获取一些值,然后将这些值插入到另一个表中。(不仅仅是这些值,还有这些值(这个值=来自基于表的值))

,我收到此错误:

System.Data.OleDb.OleDbException (0x80040E10): 没有为一个或多个必需参数给出值。`

这是代码。我不知道我错过了什么。

string selectedItem = comboBox1.SelectedItem.ToString();
Codons cdn = new Codons(selectedItem);
string codon1;
int index;

if (this.i != this.counter)
{
  //take from the DataBase the matching codonsCodon1 to codonsFullName
  codon1 = cdn.GetCodon1();

  //take the serialnumber of the last protein
  string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
         "Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
  OleDbConnection conn = new OleDbConnection(connectionString);
  conn.Open();
  string last= "SELECT proInfoSerialNum FROM tblProInfo WHERE proInfoScienceName = "+this.name ;
  OleDbCommand getSerial = new OleDbCommand(last, conn);
  OleDbDataReader dr = getSerial.ExecuteReader();
  dr.Read();
  index = dr.GetInt32(0);

  //add the amino acid to tblOrderAA
  using (OleDbConnection connection = new OleDbConnection(connectionString))
  {
    string insertCommand = "INSERT INTO tblOrderAA(orderAASerialPro, orderAACodon1) "
           + " values (?, ?)";
    using (OleDbCommand command = new OleDbCommand(insertCommand, connection))
    {
      connection.Open();
      command.Parameters.AddWithValue("orderAASerialPro", index);
      command.Parameters.AddWithValue("orderAACodon1", codon1);
      command.ExecuteNonQuery();
    }
  }
}

编辑:我在该行之后放置了一个消息框:

index = dr.GetInt32(0);

以查看问题出在哪里,在此之前我收到了错误。我没有看到消息框

I wrote some code that takes some values from one table and inserts the other table with these values.(not just these values, but also these values(this values=values from the based on table))

and I get this error:

System.Data.OleDb.OleDbException (0x80040E10): value wan't given for one or more of the required parameters.`

here's the code. I don't know what i've missed.

string selectedItem = comboBox1.SelectedItem.ToString();
Codons cdn = new Codons(selectedItem);
string codon1;
int index;

if (this.i != this.counter)
{
  //take from the DataBase the matching codonsCodon1 to codonsFullName
  codon1 = cdn.GetCodon1();

  //take the serialnumber of the last protein
  string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
         "Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
  OleDbConnection conn = new OleDbConnection(connectionString);
  conn.Open();
  string last= "SELECT proInfoSerialNum FROM tblProInfo WHERE proInfoScienceName = "+this.name ;
  OleDbCommand getSerial = new OleDbCommand(last, conn);
  OleDbDataReader dr = getSerial.ExecuteReader();
  dr.Read();
  index = dr.GetInt32(0);

  //add the amino acid to tblOrderAA
  using (OleDbConnection connection = new OleDbConnection(connectionString))
  {
    string insertCommand = "INSERT INTO tblOrderAA(orderAASerialPro, orderAACodon1) "
           + " values (?, ?)";
    using (OleDbCommand command = new OleDbCommand(insertCommand, connection))
    {
      connection.Open();
      command.Parameters.AddWithValue("orderAASerialPro", index);
      command.Parameters.AddWithValue("orderAACodon1", codon1);
      command.ExecuteNonQuery();
    }
  }
}

EDIT:I put a messagebox after that line:

index = dr.GetInt32(0);

to see where is the problem, and I get the error before that. I don't see the messagebox

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-12-25 11:59:04

您的 SELECT 命令中有语法错误,因为您没有用引号将其引起来。

string last = "SELECT proInfoSerialNum FROM tblProInfo WHERE proInfoScienceName = "+this.name ;
OleDbCommand getSerial = new OleDbCommand(last, conn);
OleDbDataReader dr = getSerial.ExecuteReader();

将其更改为

string last = "SELECT proInfoSerialNum FROM tblProInfo WHERE proInfoScienceName = ?";
OleDbCommand getSerial = new OleDbCommand(last, conn);
getSerial.Parameters.AddWithValue("?", this.name);
OleDbDataReader dr = getSerial.ExecuteReader();

Your SELECT Command has a syntax error in it because you didn't enclose it with quotes.

Change this:

string last = "SELECT proInfoSerialNum FROM tblProInfo WHERE proInfoScienceName = "+this.name ;
OleDbCommand getSerial = new OleDbCommand(last, conn);
OleDbDataReader dr = getSerial.ExecuteReader();

to

string last = "SELECT proInfoSerialNum FROM tblProInfo WHERE proInfoScienceName = ?";
OleDbCommand getSerial = new OleDbCommand(last, conn);
getSerial.Parameters.AddWithValue("?", this.name);
OleDbDataReader dr = getSerial.ExecuteReader();

此代码的示例来自此处

string SqlString = "Insert Into Contacts (FirstName, LastName) Values (?,?)";

using (OleDbConnection conn = new OleDbConnection(ConnString))
{
    using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
    {
       cmd.CommandType = CommandType.Text;
       cmd.Parameters.AddWithValue("FirstName", txtFirstName.Text);
       cmd.Parameters.AddWithValue("LastName", txtLastName.Text);
       conn.Open();
       cmd.ExecuteNonQuery();
    }
}

尝试按照示例执行相同操作。

This code is example from here:

string SqlString = "Insert Into Contacts (FirstName, LastName) Values (?,?)";

using (OleDbConnection conn = new OleDbConnection(ConnString))
{
    using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
    {
       cmd.CommandType = CommandType.Text;
       cmd.Parameters.AddWithValue("FirstName", txtFirstName.Text);
       cmd.Parameters.AddWithValue("LastName", txtLastName.Text);
       conn.Open();
       cmd.ExecuteNonQuery();
    }
}

Try to do the same as in the example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文