找不到表[0]

时间:2014-03-19 07:25:24

标签: c# sql

我试图通过将结果填入数据库来计算会计等式;但是,我试图执行它们但错误出现了

  

无法找到DataTable [0]

private void button1_Click(object sender, EventArgs e)
{
  try
  {
     //row[6]= qty.
     //row[7] =unitval.
     //row[8]=balance.
     //row[9]=avg.
     string ccc = CCC.Properties.Settings.Default.CCCConnectionString;   
     string x = string.Empty;
     x = "Insert Into [Test$]([unitval],[balance],[avg])" 
        + "Values(@unitval,@balance,@avg)";      
     using (SqlConnection c= new SqlConnection(ccc))
     {
        using (SqlCommand cmd = new SqlCommand(x,c))
        {
           c.Open();
           DataSet ds = new DataSet();      
           //DataTable dt = ds.Tables[0];               
           foreach (DataRow r in ds.Tables[0].Rows)
           {
              if (Convert.ToInt32(r[7]) != 0)
              {
                 r[8] = Convert.ToInt32(r[8]) + Convert.ToInt32(r[6]);
                 cmd.Parameters.Add(new SqlParameter("@balance", r[8]));
                 r[9] = ((Convert.ToInt32(r[6]) - Convert.ToInt32(r[8]) * Convert.ToDouble(r[9])) + (Convert.ToDouble(r[7]) * Convert.ToInt32(r[6]))) / Convert.ToInt32(r[8]) + Convert.ToInt32(r[6]);
                 cmd.Parameters.Add(new SqlParameter("@avg", r[9]));
              }
              else
              {
                 r[7] = r[9];
                 cmd.Parameters.Add(new SqlParameter("@unitval", r[9]));
                 r[8] = Convert.ToInt32(r[8]) - Convert.ToInt32(r[6]);
                 cmd.Parameters.Add(new SqlParameter("@balance", r[8]));
                 r[9] = r[9];
                 cmd.Parameters.Add(new SqlParameter("@avg", r[9]));
              }                  
              cmd.ExecuteNonQuery();
              c.Close();
              label2.Text = "Done";
           }
        }
     }
  }
  catch (Exception f)
  {
     label2.Text = "Not Inserted" + "Error: " + f.Message;
  }
}

以下是如何填充null或0数字的示例:http://imgur.com/Ch7jDyP。我试图解决这个问题,但我仍感到困惑。

已更新

        private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            string ccc = CCC.Properties.Settings.Default.CCCConnectionString;

            string x = string.Empty;
            x = "Insert Into [Test$]([unitval],[balance],[avg])" 
                + "Values(@unitval,@balance,@avg)";

            using (SqlConnection c= new SqlConnection(ccc))
            {
                using (SqlCommand cmd = new SqlCommand(x,c))
                {
                    c.Open();
                    DataSet ds = new DataSet();
                    string sql = "select * From Test$";
                    SqlDataAdapter da = new SqlDataAdapter(sql, c);
                    da.Fill(ds, "Test$");
                    //DataTable dt = ds.Tables[0];

                    foreach (DataRow r in ds.Tables[0].Rows)
                    {
                        if (Convert.ToInt32(r[7]) != 0)
                        {
                            r[8] = Convert.ToInt32(r[8]) + Convert.ToInt32(r[6]);
                            cmd.Parameters.Add(new SqlParameter("@balance", r[8]));
                            r[9] = ((Convert.ToInt32(r[6]) - Convert.ToInt32(r[8]) * Convert.ToDouble(r[9])) + (Convert.ToDouble(r[7]) * Convert.ToInt32(r[6]))) / Convert.ToInt32(r[8]) + Convert.ToInt32(r[6]);
                            cmd.Parameters.Add(new SqlParameter("@avg", r[9]));
                        }
                        else
                        {
                            r[7] = r[9];
                            cmd.Parameters.Add(new SqlParameter("@unitval", r[9]));
                            r[8] = Convert.ToInt32(r[8]) - Convert.ToInt32(r[6]);
                            cmd.Parameters.Add(new SqlParameter("@balance", r[8]));
                            r[9] = r[9];
                            cmd.Parameters.Add(new SqlParameter("@avg", r[9]));
                        }

                        cmd.ExecuteNonQuery();
                        c.Close();
                        label2.Text = "Done";
                    }


                }

            }




        }
        catch (Exception f)
        {
            label2.Text = "Not Inserted" + "Error: " + f.Message;

        }



    }

2 个答案:

答案 0 :(得分:3)

此行存在问题

DataSet ds = new DataSet();

//DataTable dt = ds.Tables[0];

您正在创建数据集,但没有填充任何数据,所以您怎么能想象它会有任何行? 尝试填写像这样的数据

string cmd="select * from some_table";
SqlDataAdapter adapter = new SqlDataAdapter(cmd, c);
adapter.Fill(ds, "some_table");

更新代码

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        string ccc = CCC.Properties.Settings.Default.CCCConnectionString;

        string x = string.Empty;
            x = "Insert Into Test$ (unitval,balance,avg)"
                + " Values(@uval,@balance,@avg)";

            using (SqlConnection c = new SqlConnection(ccc))
            {
                using (SqlCommand cmd = new SqlCommand(x, c))
                {
                    c.Open();
                    DataSet ds = new DataSet();
                    string sql = "select * From Test$";
                    SqlDataAdapter da = new SqlDataAdapter(sql, c);
                    da.Fill(ds, "Test$");
                    //DataTable dt = ds.Tables[0];

                    foreach (DataRow r in ds.Tables[0].Rows)
                    {
                        if (Convert.ToInt32(r[7]) != 0)
                        {
                            r[8] = Convert.ToInt32(r[8]) + Convert.ToInt32(r[6]);
                            cmd.Parameters.AddWithValue("@balance", r[8]);
                            r[9] = ((Convert.ToInt32(r[6]) - Convert.ToInt32(r[8]) * Convert.ToDouble(r[9])) + (Convert.ToDouble(r[7]) * Convert.ToInt32(r[6]))) / Convert.ToInt32(r[8]) + Convert.ToInt32(r[6]);
                            cmd.Parameters.AddWithValue("@avg", r[9]);
                        }
                        else
                        {
                            r[7] = r[9];
                            cmd.Parameters.AddWithValue("@uval", r[9]);
                            r[8] = Convert.ToInt32(r[8]) - Convert.ToInt32(r[6]);
                            cmd.Parameters.AddWithValue("@balance", r[8]);
                            r[9] = r[9];
                            cmd.Parameters.AddWithValue("@avg", r[9]);
                        }

                        cmd.ExecuteNonQuery();
                        c.Close();
                        label2.Text = "Done";
                    }


                }

            }




        }
        catch (Exception f)
        {
            label2.Text = "Not Inserted" + "Error: " + f.Message;

        }

答案 1 :(得分:1)

在使用其中的表格/行之前,您需要使用SqlDataAdapter 并使用Fill Method (DataSet)填充数据集。