0
我在C#中搞亂了解如何將記錄插入到使用C#ODBC的數據庫中。我已經學會了如何將記錄讀入DGV
,但是現在我陷入了插入。 我的代碼正在做什麼的快速概覽,它從表中讀取20行到DGV
,然後它應該將相同的行插入到不同的表中。使用c#odbc插入記錄在oracle數據庫中
我使用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
。我試圖研究這一點,但不介意任何幫助我的信息,任何推向正確的方向將不勝感激。
那麼有什麼不工作?你有錯誤嗎? – stuartd
嗯,我得到消息框指出「錯誤2」,因爲它在SetData方法下。去除捕獲物並查看我通常會得到的東西會更有利嗎? – user2405778
在Error 2消息框之前,您會發現異常。 Message屬性包含什麼? – stuartd