使用c#odbc在oracle数据库中插入记录

使用c#odbc在oracle数据库中插入记录,c#,oracle,insert,odbc,C#,Oracle,Insert,Odbc,我在C#中混日子,试图了解如何使用C#ODBC将记录插入数据库。我已经学会了如何将记录读入DGV,但现在我在插入时遇到了困难。 快速概述我的代码正在执行的操作,将代码从表中读取20行到DGV,然后将相同的行插入到不同的表中 我正在使用VS 2012和SQL Developer(Oracle) 以下是我表格中的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.D

我在C#中混日子,试图了解如何使用C#ODBC将记录插入数据库。我已经学会了如何将记录读入
DGV
,但现在我在插入时遇到了困难。 快速概述我的代码正在执行的操作,将代码从表中读取20行到
DGV
,然后将相同的行插入到不同的表中

我正在使用VS 2012和SQL Developer(Oracle)

以下是我表格中的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        for (int rows = 0; rows < 20; rows++)
        {
            dataGridView1.Rows.Add(); // adding needed amount of rows

            for (int cols = 0; cols < 13; cols++)
            {
                this.dataGridView1[cols, rows].Value = getNumberOfThreads(rows, cols);
            }
        }
        StringBuilder sql = new StringBuilder();

        sql.AppendLine("insert into chsarp_test_table");
        sql.AppendLine("SELECT *");
        sql.AppendLine("FROM legal_transactions");
        sql.AppendLine("WHERE rownum between 1 and 25");

        //using (DataTable dt = Database.GetData(sql.ToString()))
        //    if (dt.Rows.Count > 0)
        //  Database dt = new Database.SetData(sql.ToString());
        Database.SetData(sql.ToString());
    }

    public static string getNumberOfThreads(int i, int j)
    {

        StringBuilder sql = new StringBuilder();
        sql.AppendLine("SELECT *");
        sql.AppendLine("FROM legal_transactions");
        sql.AppendLine("WHERE rownum between 1 and 25");

        using (DataTable dt = Database.GetData(sql.ToString()))
            if (dt.Rows.Count > 0)
                return dt.Rows[i][j].Equals(DBNull.Value) ? "null" : dt.Rows[i][j].ToString();

        return "null";
    }
}

}

我试图单步遍历代码,它在
ExecuteNonQuery
上的
SetData
方法中出现。我试图调查此事,但不介意有任何信息对我有所帮助,如果能朝着正确的方向努力,我将不胜感激

我在插入的表上缺少架构。在问题评论部分@stuartd的帮助下发现了这一点。

那么什么不起作用呢?你收到错误了吗?我收到一个消息框,上面写着“错误2”,因为它在SetData方法中出现了。删除捕获并查看我通常会得到什么会更有益吗?在错误2 messagebox之前,您捕获了一个异常。Message属性包含什么?啊,我已经注释掉了,让我再次运行它,并在消息框中显示它。唉,我怎么能给你一个rep来回答我愚蠢的错误呢。感谢您指出这个例外,我显然忘记添加模式了(尽管它是我的,我想我不需要添加它)。现在可以了,非常感谢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Odbc; //used for the ODBC stuff
using System.Data; // Used for public static datatable
using System.Windows.Forms;

namespace WindowsFormsApplication13
{
class Database
{

    private const string connOdbc = "dsn=atlas32;uid=NAME;pwd=XXXX";
    private const string cnnOLE = "provider =XXXX;User ID = NAME;password = XXXX; Data Source = XXX Properties=;Persist Security Info=False";

    public static DataTable GetData(string Sql)
    {
        DataTable dt = new DataTable();
        try
        {
            OdbcConnection cnn = GetConnection();
            using (OdbcDataAdapter da = new OdbcDataAdapter(Sql, cnn))
            {
                da.SelectCommand.CommandTimeout = 0;
                da.Fill(dt);
            }
            CloseConnection(cnn);
        }
        catch (Exception ex)
        {
            //Queries.LogErrors(ex.Message, Sql);
            MessageBox.Show("Error 1");

        }
        return dt;
    }

    public static void SetData(string sql)
    {

        try
        {
            OdbcConnection cnn = GetConnection();
            using (OdbcCommand cmd = new OdbcCommand(sql, cnn))
                cmd.ExecuteNonQuery();

            CloseConnection(cnn);
        }
        catch (Exception ex)
        {
            //Queries.LogErrors(ex.Message, sql);
            MessageBox.Show("Error 2");
        }

    }

    private static OdbcConnection GetConnection()
    {
        try
        {
            OdbcConnection cnn = new OdbcConnection() { ConnectionString = connOdbc };
            cnn.Open();
            return cnn;
        }
        catch (Exception ex)
        {
            //throw ex;
        }
        return null;
    }

    private static void CloseConnection(OdbcConnection Connection)
    {
        try
        {
            if (Connection.State == ConnectionState.Open)
                Connection.Close();
                Connection.Dispose();
        }
        catch (Exception ex)
        {
            //throw ex;
        }
        Connection = null;
    }

}

}