How i can insert more than one rows into more than one tables?? ( in one mehtod ) when user click 'Send Message Button'
hi all , i want to know how i can insert many rows into many tables in one method , such as the following Scenario :
1 - if u want to send compose message to more than one Receiver , u select more than receiver( Receiver table) , and attach more than Files ( File Table) ...... etc , in one method .
how ?? any example? plz im new programmer

Kirtan PatelRank #292 · 8.6K reputationPosted Aug 8, 2009, 12:37 PM EDT
in Array or generic collection like List
in Most of code i have explained what the code does but if you still not get you are welcome to ask Question :)
private void button1_Click(object sender, EventArgs e) { Liststring> UsersList = new Liststring>(); SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"); con.Open(); SqlCommand comm = new SqlCommand("select Username from Users", con); SqlDataReader reader = comm.ExecuteReader(); //Clear the UserList before Filling new UsersList.Clear(); //Loop through all The users and Add Them to list For Sending while (reader.Read()) { UsersList.Add(reader["Username"].ToString()); } // Now Execute insert Query on All Usernames in List foreach (string user in UsersList) { comm = new SqlCommand("Insert into Users(Username,Message) values('" + user + "','testmsg')", con); comm.ExecuteNonQuery(); } //Finaly Close the Connection con.Close(); }abas shahen50 reputationPosted Aug 8, 2009, 12:45 PM EDT
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 8, 2009, 12:55 PM EDT
It depends on situation what to use From DataReader , Dataset
Data Reader just Goes into One Direction when iterating through record and need Database Connection Open when Performing Operation while data set has several advantages over data reader like
it can move in both direction in records Forward and backward and no need Database Connection once after data set is filled
But When you have to do small task like no more operation that can round trip the Database you can use DataReader
But When Performing Search on Database You should avoid using DataReaders
Thank you :)
abas shahen50 reputationPosted Aug 8, 2009, 1:00 PM EDT