Click here to Skip to main content
16,013,642 members
Home / Discussions / C#
   

C#

 
GeneralRe: Passing Value Types by Reference Pin
Heath Stewart22-Dec-04 19:17
protectorHeath Stewart22-Dec-04 19:17 
GeneralRe: Passing Value Types by Reference Pin
Salil Khedkar22-Dec-04 19:20
Salil Khedkar22-Dec-04 19:20 
GeneralProblem with Stored Procedure Pin
ronin177022-Dec-04 14:29
ronin177022-Dec-04 14:29 
GeneralRe: Problem with Stored Procedure Pin
Javier Lozano22-Dec-04 18:36
Javier Lozano22-Dec-04 18:36 
Generalproblem auto increment field Pin
Gedrain22-Dec-04 13:30
Gedrain22-Dec-04 13:30 
GeneralRe: problem auto increment field Pin
Skynyrd22-Dec-04 13:35
Skynyrd22-Dec-04 13:35 
GeneralRe: problem auto increment field Pin
Heath Stewart22-Dec-04 19:07
protectorHeath Stewart22-Dec-04 19:07 
GeneralRe: problem auto increment field Pin
Gedrain23-Dec-04 3:13
Gedrain23-Dec-04 3:13 
Well i inserted at the insert of the table brands the extra sql statement
"select brand_id, name from brands" and now i get a error on the update methode

adapterBrands.Update(dS.GetChanges(), "brands");

error: "Characters found after end of SQL statement."


string ConnectionString = @"Provider=Microsoft Jet 4.0 OLE DB Provider;Data Source=C:\test\test_Techno1.mdb;";
System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(ConnectionString);
connection.Open();

string strSelectProducts = "SELECT PRODUCT_ID,NAME,PRICE,PRODUCT_CODE,BRAND FROM PRODUCTS";
string strInsertProducts = "INSERT INTO PRODUCTS (NAME,PRICE,PRODUCT_CODE,BRAND) VALUES (?, ?, ?, ?)";

System.Data.Common.DataColumnMapping[] dataColumnMappingProducts = (System.Data.Common.DataColumnMapping[]) Array.CreateInstance( typeof(System.Data.Common.DataColumnMapping), 5 );

dataColumnMappingProducts[0] = new System.Data.Common.DataColumnMapping("PRODUCT_ID","PRODUCT_ID");
dataColumnMappingProducts[1] = new System.Data.Common.DataColumnMapping("NAME,","NAME,");
dataColumnMappingProducts[2] = new System.Data.Common.DataColumnMapping("PRICE","PRICE");
dataColumnMappingProducts[3] = new System.Data.Common.DataColumnMapping("PRODUCT_CODE","PRODUCT_CODE");
dataColumnMappingProducts[4] = new System.Data.Common.DataColumnMapping("BRAND","BRAND");


OleDbTransaction transaction = null;

OleDbDataAdapter adapterProducts = new OleDbDataAdapter();

adapterProducts.SelectCommand = new OleDbCommand(strSelectProducts, connection, transaction);
adapterProducts.InsertCommand = new System.Data.OleDb.OleDbCommand(strInsertProducts, connection, transaction);

adapterProducts.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {new System.Data.Common.DataTableMapping("products", "products", dataColumnMappingProducts)});

OleDbCommandBuilder cbProducts = new OleDbCommandBuilder(adapterProducts);

DataSet dS = new DataSet();

adapterProducts.Fill(dS, "products");

adapterProducts.FillSchema(dS, System.Data.SchemaType.Mapped, "products");

adapterProducts.InsertCommand.Parameters.Add(new OleDbParameter("name", System.Data.OleDb.OleDbType.VarChar , 50 , "name"));
adapterProducts.InsertCommand.Parameters.Add(new OleDbParameter("price", System.Data.OleDb.OleDbType.Currency , 20 , "PRICE"));
adapterProducts.InsertCommand.Parameters.Add(new OleDbParameter("product_code", System.Data.OleDb.OleDbType.VarChar , 50 , "product_code"));
adapterProducts.InsertCommand.Parameters.Add(new OleDbParameter("brand", System.Data.OleDb.OleDbType.VarChar , 20 , "brand"));


string strSelectBrands = "SELECT BRAND_ID, NAME FROM BRANDS";
string strInsertBrands = "INSERT INTO BRANDS (NAME) VALUES (?);select brand_id, name from brands";

OleDbDataAdapter adapterBrands = new OleDbDataAdapter();

System.Data.Common.DataColumnMapping[] dataColumnMappingBrands = (System.Data.Common.DataColumnMapping[]) Array.CreateInstance( typeof(System.Data.Common.DataColumnMapping), 1);

dataColumnMappingBrands[0] = new System.Data.Common.DataColumnMapping("NAME","NAME");

adapterBrands.SelectCommand = new OleDbCommand(strSelectBrands, connection, transaction);
adapterBrands.InsertCommand = new System.Data.OleDb.OleDbCommand(strInsertBrands, connection, transaction);


adapterBrands.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {new System.Data.Common.DataTableMapping("brands", "brands", dataColumnMappingBrands)});

adapterBrands.InsertCommand.Parameters.Add(new OleDbParameter("name", System.Data.OleDb.OleDbType.VarChar , 50 , "name"));

OleDbCommandBuilder cbBrands = new OleDbCommandBuilder(adapterBrands);

adapterBrands.Fill(dS, "brands");

adapterBrands.FillSchema(dS, System.Data.SchemaType.Mapped, "brands");

dS.Relations.Add(dS.Tables["brands"].Columns["brand_id"], dS.Tables["products"].Columns["brand"]);

try
{
DataRow drBrand = dS.Tables["brands"].NewRow();
drBrand["name"] = "nieuwe naam";
dS.Tables["brands"].Rows.Add(drBrand);


adapterBrands.InsertCommand.Transaction = connection.BeginTransaction();
adapterBrands.Update(dS.GetChanges(), "brands"); // <--- i get a error here after inserting the "; select id, name from brands" in the sql insert statement for table brands error: "Characters found after end of SQL statement."

adapterBrands.InsertCommand.Transaction.Commit();

DataRow drProduct = dS.Tables["products"].NewRow();
drProduct["name"] = "nieuw product";
drProduct["product_code"] = "product_code";
drProduct["price"] = "7,5";
drProduct["brand"] = drBrand["brand_id"];
dS.Tables["products"].Rows.Add(drProduct);


adapterProducts.InsertCommand.Transaction = connection.BeginTransaction();
adapterProducts.Update(dS.GetChanges(), "products");
adapterProducts.InsertCommand.Transaction.Commit();


}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());

}
finally
{

}


Greetings

Sander
GeneralRe: problem auto increment field Pin
Heath Stewart23-Dec-04 4:59
protectorHeath Stewart23-Dec-04 4:59 
GeneralTextBox Pin
Aviv Halperin22-Dec-04 11:56
Aviv Halperin22-Dec-04 11:56 
GeneralRe: TextBox Pin
Heath Stewart22-Dec-04 12:11
protectorHeath Stewart22-Dec-04 12:11 
GeneralRe: TextBox Pin
Aviv Halperin22-Dec-04 12:17
Aviv Halperin22-Dec-04 12:17 
GeneralRe: TextBox Pin
Heath Stewart22-Dec-04 12:40
protectorHeath Stewart22-Dec-04 12:40 
GeneralRe: TextBox Pin
Aviv Halperin22-Dec-04 12:20
Aviv Halperin22-Dec-04 12:20 
GeneralRe: TextBox Pin
Skynyrd22-Dec-04 12:29
Skynyrd22-Dec-04 12:29 
GeneralRe: TextBox Pin
Aviv Halperin22-Dec-04 12:39
Aviv Halperin22-Dec-04 12:39 
GeneralRe: TextBox Pin
Skynyrd22-Dec-04 12:46
Skynyrd22-Dec-04 12:46 
GeneralRe: TextBox Pin
Heath Stewart22-Dec-04 12:50
protectorHeath Stewart22-Dec-04 12:50 
GeneralRe: TextBox Pin
Aviv Halperin22-Dec-04 13:13
Aviv Halperin22-Dec-04 13:13 
GeneralRe: TextBox Pin
Heath Stewart22-Dec-04 13:18
protectorHeath Stewart22-Dec-04 13:18 
GeneralRe: TextBox Pin
Aviv Halperin22-Dec-04 13:20
Aviv Halperin22-Dec-04 13:20 
GeneralRe: TextBox Pin
Matt Gerrans22-Dec-04 12:31
Matt Gerrans22-Dec-04 12:31 
GeneralRe: TextBox Pin
Aviv Halperin22-Dec-04 12:41
Aviv Halperin22-Dec-04 12:41 
GeneralRe: TextBox Pin
Heath Stewart22-Dec-04 12:43
protectorHeath Stewart22-Dec-04 12:43 
GeneralRe: TextBox Pin
Aviv Halperin22-Dec-04 12:49
Aviv Halperin22-Dec-04 12:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.