the basics steps in DISCONNECTED way ?? plz help me  
Author Message
holy_spirit





PostPosted: .NET Framework Data Access and Storage, the basics steps in DISCONNECTED way ?? plz help me Top

hi all ... here is my problem
i have a database created with access
and i wanna get the data from it by using
disconnected way in ado.net

so I create oleDbDataAdapter
then i fill the dataset by using the oleDbDataAdapter1.Fill(dataSet1);method
and then i created a DataTable and i gave it same name that i called in the access db
and by using while loop i tryed to get the data
with read() method
but no result

So plz can any body exlpain what is the basic steps that can allow md bring data from db and put it in Dataset, then display it on RichTextbox or what ever :S
help me plz !!
NOTE : SORRY ABOUT MY BAD LANGUAGE AND GRAMMAR



.NET Development12  
 
 
Matt Neerincx





PostPosted: .NET Framework Data Access and Storage, the basics steps in DISCONNECTED way ?? plz help me Top

Here you go, here is a simple example to get you started:

// Open connection to my access database.

// DataAdapter takes select statement that generates your table and connection.
OleDbDataAdapter da = new OleDbDataAdapter("select * from customers", conn);

// You can call the dataset anything, it's not important.
DataSet ds = new DataSet("MyDataSet");

// When calling fill, give it the table name you want here. What this means is select * from customers from above is fed into table called customers in the dataset. But if you want to rename it here it's ok, but for simplicity I leave it the same.
da.Fill(ds, "customers");
da.Dispose();
conn.Dispose();

// Now dataset has the disconnected data.
// You can get to the table directly like so:
DataTable customers = ds.Tables["Customers"];

// Loop thru records in customers table.
foreach (DataRow r in customers.Rows)
{
for (int i=0; i<customers.Columns.Count; i++)
{
Console.WriteLine(rIdea.ToString());
}
}

// Or you can assign datatable to grid control, etc...
dataGrid1.DataSource = customers;



 
 
holy_spirit





PostPosted: .NET Framework Data Access and Storage, the basics steps in DISCONNECTED way ?? plz help me Top

thanx man ... :D
 
 
holy_spirit





PostPosted: .NET Framework Data Access and Storage, the basics steps in DISCONNECTED way ?? plz help me Top

help me again man plz

sorry man but seems that i cant update my datasource by my self
when i was looking for something useful in THE MSDN HELP (on my pc )
I didnt find any thing useful cuz when I created my dataset and the other attachemnts
I did that by hand of design wizards and all update topics talk about updating by using
dataTable and DataRow...and so on,realy I cant understand any thing
so plz tell me how can i update my datasource cuz i found that there are alot of overloads methods for the OleDBAdapter.Update();

sorry again about my bad grammar and language
thanx . :D


 
 
Matt Neerincx





PostPosted: .NET Framework Data Access and Storage, the basics steps in DISCONNECTED way ?? plz help me Top

If you are using built in databinding then you should not need to write any code. Just add a navigator to the form and hook it up and there is a save button on the navigator (I think).

 
 
holy_spirit





PostPosted: .NET Framework Data Access and Storage, the basics steps in DISCONNECTED way ?? plz help me Top

no man .. i realy dont need to use navigator cuz i dont havr datagrid ...

i just have some date in the textboxes and i wanna update my datasource depends on that data that is

so what should i do

BTW thanx alot man your amazing :D


 
 
Matt Neerincx





PostPosted: .NET Framework Data Access and Storage, the basics steps in DISCONNECTED way ?? plz help me Top

To get the data back into the database you need to update the dataset then use a OledbDataAdapter class to perform the work of syncing the changes in the DataSet to the database.

In order to do this you need to create an InsertCommand, UpdateCommand, and DeleteCommand for the OledbDataAdapter and setup parameters for the values you want to use.

For a DeleteCommand, this only really needs the primary key value, so this one is simple.

For the InsertCommand, you need to provide parameters for every single value you want to insert as well as primary key. Same goes for UpdateCommand.

Alot of newbies use the OledbCommandBuilder class to help create the appropriate OledbDataAdapter automagically, so you may want to look at this.



 
 
holy_spirit





PostPosted: .NET Framework Data Access and Storage, the basics steps in DISCONNECTED way ?? plz help me Top

hi man

//GET THE INFO FROM DATA SORUCE THE FILL THE DATA SET

public void GetAdminInfo()

{

da.Fill(ds, "admin");

DataTable admin = ds.Tables[0];

DataRow r = admin.Rows[0];

this.adminInfo[0] = Convert.ToString(r[0]);

this.adminInfo[1] = Convert.ToString(r[1]);

}

then when SAVE btn pressed

// CHECK USERNAME AND PASSWORD THEN TRY TO UPDATE DATASOURCE

private void btnSave_Click(object sender, EventArgs e)

{

if (txtOldPass.Text == this.adminInfo[1] && txtUserName.Text == this.adminInfo[0])

{

//NOT SURE IF THE IS THE RIGHT WAY

OleDbCommand update = new OleDbCommand("UPDATE ADMIN SET ( USERNAME ='123', PASSWORD=123)");

da.UpdateCommand = update;

da.Update(ds);

}

}

}

but nothing new .... datasource still normal no changes !!!!!!!!!!


 
 
holy_spirit





PostPosted: .NET Framework Data Access and Storage, the basics steps in DISCONNECTED way ?? plz help me Top

hiiii plz read it plz help me !!!!
 
 
Matt Neerincx





PostPosted: .NET Framework Data Access and Storage, the basics steps in DISCONNECTED way ?? plz help me Top

Here is a basic sample:

SqlConnection conn = new SqlConnection(CONNECT);
conn.Open();

// Create simple admin table with a few users. I commented this out but this demonstrates my table structure
//xsql_noerror(conn, "drop table admin");
//xsql(conn, "create table admin (username varchar(256) primary key, password varchar(256))");
//xsql(conn, "insert into admin (username,password) values ('matt', 'matt')");
//xsql(conn, "insert into admin (username,password) values ('shu', 'shu')");

// Create data adapter to fill our dataset.
SqlDataAdapter da = new SqlDataAdapter("select * from admin", conn);
DataSet ds = new DataSet("MyAdminSet");
da.Fill(ds, "admin");

// Ok, now I have my records, say I want to change password for matt.
DataTable t = ds.Tables["admin"];
t.PrimaryKey = new DataColumn [] { t.Columns["username"] };
DataRow r = t.Rows.Find(new object [] {"shu"});

// Change password.
r["password"] = "SomeNewPassword";

// Now lets update our table.
SqlCommand cmdUpdate = conn.CreateCommand();
cmdUpdate.CommandType = CommandType.Text;
";


da.UpdateCommand = cmdUpdate;
da.Update(ds, "admin");

// Now read back the rows to verify we updated it.
SqlCommand cmdTest = conn.CreateCommand();
cmdTest.CommandType = CommandType.Text;
cmdTest.CommandText = "select * from admin where username='shu'";

SqlDataReader dr = cmdTest.ExecuteReader();
dr.Read();
System.Diagnostics.Debug.WriteLine("username=" + dr[0].ToString() + " password=" + dr["password"].ToString());
dr.Dispose();
conn.Close();