Beginning to use MySQL with C#
Hi All,
I have created a database called "infrastk" with a table called "tblUno". This table has a structure as
follows:
field, type(size)
-----------------
cCod, varchar(6)
cDenom, varchar(27)
I try to append a record to the table "tblUno" by means of a C# program:
using System;
using System.Data;
using MySql.Data.MySqlClient;
class MainClass
{
public static void Main()
{
// Code to connect with database and create a dataset
// and fill the dataset with the data in the table "tblUno" --
MySqlConnection myConn = new MySqlConnection(
"user id=griv; password=mypw; database=infraestructura; server=localhost");
MySqlCommand myCommand = new MySqlCommand("select * from tblUno", myConn);
MySqlDataAdapter myDA = new MySqlDataAdapter(myCommand);
DataSet myDS = new DataSet();
myDA.Fill(myDS, "dsUno");
// Code to add a row to the table dsUno in the dataset "dsUno" --
DataRow myRow = myDS.Tables["dsUno"].NewRow();
myRow["cCod"] = "ABC01";
myRow["cDenom"] = "Machine 1";
myDS.Tables["dsUno"].Rows.Add(myRow);
// Code to verify that the former code had success --
foreach (DataColumn column in myDS.Tables["dsUno"].Columns)
Console.WriteLine("{0}", column.Caption);
foreach(DataTable myTable in myDS.Tables){
foreach(DataRow myRenglon in myTable.Rows){
foreach (DataColumn myColumn in myTable.Columns)
Console.WriteLine(myRenglon[myColumn]);
}
}
// Code to update the table "tblUno" with "dsUno" --
MySqlCommandBuilder mBuild = new MySqlCommandBuilder(myDA);
myDA.Update(myDS, "dsUno");
}
}
*** It compiles without errors but when it runs, it sends the message shown bellow indicating a big
problem to update the table "tblUno".
cCod
cDenom
ABC01
Machine 1
Unhandled Exception: System.InvalidOperationException: Connection must be valid
and open
at MySql.Data.MySqlClient.MySqlCommand.CheckState()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior
)
at MySql.Data.MySqlClient.MySqlCommandBuilder.GenerateSchema()
at MySql.Data.MySqlClient.MySqlCommandBuilder.OnRowUpdating(Object sender, My
SqlRowUpdatingEventArgs args)
at MySql.Data.MySqlClient.MySqlDataAdapter.OnRowUpdating(RowUpdatingEventArgs
value)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMappi
ng tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
at ManejoDatos.uno() in c:\aplics\estudio\dotnet\cnnet_adonet\Main.cs:line 54
at MainClass.Main() in c:\aplics\estudio\dotnet\cnnet_adonet\Main.cs:line 15
Press any key to continue . . .
*** I will appreciate any kind of help to discover where am I wrong.
Have a nice weekend!
griv
Edited 4 time(s). Last edit at 10/21/2005 12:21PM by Gerardo Antonio Rivera Rivera.
Subject
Written By
Posted
Beginning to use MySQL with C#
October 21, 2005 10:42AM
November 02, 2005 02:40PM
November 03, 2005 11:33AM
November 11, 2005 03:17PM
December 13, 2005 04:15AM
December 14, 2005 11:22AM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.