Send data from one data table to other - sql

I am trying to send data from sql to oracle using bulk copy.
Here is the code
string sqldb = "server=DOR-SQLTEST\\DORSQLSERVER;" + "initial catalog=RD2KTEST;" + "user id=amehra;" + "password=Research#322";
SqlConnection sqlconn = new SqlConnection(sqldb);
sqlconn.Open();
string sqlstring="select TOP 10 POSITION_ID, POSITION_DESC from T_CD_POSITION";
SqlCommand scmd = new SqlCommand(sqlstring, sqlconn);
SqlDataReader reader = scmd.ExecuteReader();
while(reader.Read())
{
Console.WriteLine("entered to write to oracle.......\n\n");
string oradb = "Data Source=UHAMPTST;user id=ocg_de;password=rd2k";
OracleConnection oraconn = new OracleConnection(oradb);
oraconn.Open();
Console.WriteLine("orca conn established....");
using( OracleBulkCopy orca_bulk_copy = new OracleBulkCopy(oraconn))
{
orca_bulk_copy.DestinationTableName = "t_test_postion";
orca_bulk_copy.WriteToServer(reader);//line 38
orca_bulk_copy.Close();
orca_bulk_copy.Dispose();
}
}
}
}
}
I'm getting error at line 38 as Oracle Exception occurred.
Kindly rectify the problem

Related

How to query SQL to RichEditControl DevExpress?

I want to fill data from SQL Server to RichEditControl.Text. I used this code but it doesn't work, my RichEditControl.Text has an empty string.
string connectionstring = "Data Source=ADMIN;Initial Catalog=Shoplacviet;Integrated Security=True";
SqlConnection conn = new SqlConnection(connectionstring);
conn.Open();
string query = "select * from so";
SqlCommand cmd = new SqlCommand(query, conn);
var reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
richEditControl1.Options.MailMerge.DataSource = dt;
richEditControl1.Options.MailMerge.ViewMergedData = true;
richEditControl1.Text = dt.ToString();
conn.Close();

How do I cast decimal values to string from my database?

The below code snippet pulls a pack size column from my table but am using getString which I get an error then now see is an issue because the column am pulling is of decimal type. How would I cast my snippet to accept the decimal values?
try
{
string connectionString = "Data Source=CMDLAP121\\SQLEXPRESS;Initial Catalog=TESTBAR;Integrated Security=True";
string query = "SELECT * FROM BCODEREF WHERE BAR_CODE = '" + comboBox1.Text + "';";
string mystring = query;
SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string spo = dr.GetString(dr.GetOrdinal("PACKSIZE"));
ponum.Text = spo;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Parse to decimal first and convert to a string with the format you need:
decimal packSize= 0;
decimal.TryParse(dr["PACKSIZE"].ToString(), out packSize);
ponum.Text = packSize.ToString("0.00"); //specify format you need
See this post for details of how to set up appropriate string format

Adding new data error

When i'm running this code, it says:
String or binary data would be truncated. The statement has been
terminated.
On the underlined area, can some one help me with this.
namespace ASP_05
{
public partial class AddEmployee : System.Web.UI.Page
{
public DataSet getDataset(string query)
{
String sConn = "Data Source=(local);Initial Catalog=medewerkers;Integrated Security=SSPI;";
SqlConnection conn = new SqlConnection(sConn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(query, conn);
da.Fill(ds);
da.Dispose();
return ds;
}
protected void toevoegen_Click(object sender, EventArgs e)
{
String vn = voornaam.ToString();
String an = voornaam.ToString();
String a = voornaam.ToString();
String t = voornaam.ToString();
string query = "INSERT INTO tblMedewerkers (voornaam, achternaam, afdeling, toestelnummer) VALUES ('" + vn + "','" + an + "','" + a + "','" + t + "')";
this.execSQL(query);
}
public int execSQL(string query)
{
String sConn = "Data Source=(local);Initial Catalog=medewerkers;Integrated Security=SSPI;";
SqlConnection conn = new SqlConnection(sConn);
conn.Open();
SqlCommand cmd = new SqlCommand(query, conn);
int i = cmd.ExecuteNonQuery();
conn.Close();
return i;
}
}
}
The data that you are inserting in the table for a particular column is more in size than what you have defined in the table. Hence you are getting this error. Go through your column definitions and also through the records you are inserting and check if any of the column have less size than data you are inserting

"Invalid object name" error in adapter.Fill method

I'm trying to make a DataSet from all tables of the AdventureWorks2012 database. But I'm getting an "Invalid object name " error in the adapter.Fill method.
Any ideas?
private void openDBButton_Click(object sender, RoutedEventArgs e)
{
DataSet myDataSet = new DataSet();
SqlDataAdapter myDataAdapter;
SqlCommand myCommand;
string conStr = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + dbPath.Text +
";Integrated Security=true";
SqlConnection connection = new SqlConnection(conStr);
connection.Open();
DataTable tables = connection.GetSchema("Tables");
foreach (DataRow table in tables.Rows)
{
if (table["TABLE_TYPE"].ToString() != "BASE TABLE")
continue;
string tableName = table["TABLE_NAME"].ToString();
string sqlCmd = "SELECT * FROM " + "[" + tableName + "]";
//SqlDataAdapter adapt = new SqlDataAdapter(new SqlCommand(sqlCmd, connection));
//adapt.FillSchema(myDataSet, SchemaType.Mapped, tableName);
myCommand = new SqlCommand(sqlCmd, connection);
myDataAdapter = new SqlDataAdapter(myCommand);
myDataAdapter.Fill(myDataSet, tableName); // here i'm get in trouble :)
}
}

How to get a dataset value

New to VB.Net,
How to insert or select the dataset value.
cmd = New SqlCommand("Select * from table1", con)
ada = New SqlDataAdapter(cmd)
ds = New DataSet
ada.Fill(ds)
cmd = New SqlCommand("Select * from '" & ds.Tables(0) & "' ", con)
mydatatable1.Load(dr3)
It was showing the error in '" & ds.Tables(0) & "', I want to get the dataset value
Need VB.Net Code Help
You have a reasonable idea right up until you get to the point where you are trying to create a second SqlCommand. That is, once you do the Fill, you already have the data in a table. You wouldn't run another select - you've already done that. You'd just reference the table that you want to use in the dataset.
If you want a data table you would do something like this (for VB, see below):
SqlDataAdapter myAdapter = new SqlDataAdapter(CommandText, con);
DataSet myDataset = new DataSet();
myAdapter.Fill(myDataset, "Table"); // "Table" is just a name, it can be anything.
mydatatable1 = myDataset.Tables[0]; // Get the table
Now, if you don't need a DataTable, per se, but just want to read from the query, you'd use a SqlDataReader:
cmd = new SqlCommand(CommandText, con);
// One or more params
cmd.Parameters.AddWithValue("#paramName", Value);
SqlDataReader nwReader = cmd.ExecuteReader();
Then just read from the nwReader:
while (nwReader.Read())
{
string field1Val = (string)nwReader["FieldName"];
etc...
}
Update: I don't know VB but here is what I think it would look like:
cmd = New SqlCommand("Select * from table1", con)
ada = New SqlDataAdapter(cmd)
ds = New DataSet
ada.Fill(ds)
mydatatable1 = ds.Tables(0);
You may well be able to shorten this to get rid of the extra SqlCommand (assuming VB supports this SqlDataAdapater syntax like C#.
ada = New SqlDataAdapter("Select * from table1", con)
ds = New DataSet
ada.Fill(ds)
mydatatable1 = ds.Tables(0);
Good luck...
you can use Microsoft Enterprise Library for making easy this process. this library is a powerful library for working with datasets.
public async Task<ResponseStatusViewModel> GetAll()
{
var responseStatusViewModel = new ResponseStatusViewModel();
var connection = new SqlConnection(EmployeeConfig.EmployeeConnectionString);
var command = new SqlCommand("usp_GetAllEmployee", connection);
command.CommandType = CommandType.StoredProcedure;
try
{
await connection.OpenAsync();
var reader = await command.ExecuteReaderAsync();
var dataSet = new DataSet();
dataSet.Load(reader, LoadOption.OverwriteChanges, new string[] { "Employee" });
reader.Close();
reader.Dispose();
var employees = new List<EmployeeModel>();
if (dataSet.Tables.Count > 0 && dataSet.Tables["Employee"] != null)
{
var employeeTable = dataSet.Tables["Employee"];
for (int i = 0; i < employeeTable.Rows.Count; i++)
{
var employeeRow = employeeTable.Rows[i];
employees.Add(new EmployeeModel
{
EmployeeID = Convert.ToInt32(employeeRow["EmployeeID"]),
EmployeeName = Convert.ToString(employeeRow["EmployeeName"]),
Address = Convert.ToString(employeeRow["Address"]),
GrossSalary = Convert.ToDouble(employeeRow["GrossSalary"]),
PF = Convert.ToDouble(employeeRow["PF"]),
TotalSalary = Convert.ToDouble(employeeRow["TotalSalary"])
});
}
}
responseStatusViewModel.Data = employees;
command.Dispose();
connection.Dispose();
}
catch (Exception ex)
{
throw ex;
}
return responseStatusViewModel;
}
I think what you are looking for is
cmd = New SqlCommand("Select * from '" & ds.Tables(0).TableName & "' ", con)
cmd = New SqlCommand("Select * from table1", con)
ada = New SqlDataAdapter(cmd)
ds = New DataSet
ada.Fill(ds)
cmd = New SqlCommand("Select * from '" & ds.Tables(0).TableName & "' ", con)
mydatatable1.Load(dr3)