我在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";
}
}
}
以下是我班级的代码:
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;
}
}
}
我尝试逐步执行代码,然后SetData
Method
上的ExecuteNonQuery
{{1}}。我试图调查这个,但无法想到任何有助于我的信息,任何正确方向的推动都会非常感激。
答案 0 :(得分:0)
我在插入的桌子上错过了Schema。在@stuartd的帮助下在问题的评论部分找到了这个。