private void cmddelete_Click(object sender, EventArgs e) { DataRowView drv = (DataRowView)grid1.SelectedRows[0].DataBoundItem; int id = int.Parse(drv.Row["id"].ToString()); Command1.Connection = Connection1; Command1.CommandText = "DELETE FROM rahnimaghaze WHERE ID = " + id.ToString(); Connection1.Open(); Command1.ExecuteNonQuery(); Connection1.Close(); BindData(); }
private void BindData() { //Connection1.ConnectionString = ConfigurationSettings.AppSettings["connectionstring"].ToString(); Connection1.ConnectionString = "Data Source=.;Initial Catalog=amlak;Integrated Security=True"; Connection1.Open(); Adapter1.SelectCommand.Connection = Connection1; Adapter1.SelectCommand.CommandText = "SELECT * FROM jadidzamin"; DataTable dt = new DataTable(); Adapter1.Fill(dt); grid1.DataSource = dt; Connection1.Close(); }
public int DataSelect(string query) { // initialize variables int rows = 0; // create a connection string for the database //ConnectionStringSettings settings; //settings = System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseConnection"]; //string connectionString = settings.ConnectionString; // create a connection object //SqlConnection Connection1; //Connection1 = new SqlConnection(); //Connection1.ConnectionString = connectionString; // create db connection object SqlConnection Connection1; Connection1 = createDBConnection(); // use try block to fail gracefully try { // open a connection to the database Connection1.Open(); Debug.WriteLine("connection opened to select"); // create an sqlcommand object with the query and database connection SqlCommand command = new SqlCommand(query, Connection1); // create a sql reader object with the sql command object SqlDataReader reader = command.ExecuteReader(); // only read if there is valid information to be read if (reader.HasRows) { // initialize variable to iterate over reader results int i = 1; // while the reader has rows while (reader.Read()) { // since there are 7 elements in each row of the table, bet we do not wanbt the first (i=0) while (i < 7) { // output the information from the reader Debug.WriteLine(reader[i]); // increment i i += 1; } } } // close the connection to the database Connection1.Close(); Debug.WriteLine("connection closed"); // return the number of rows affected return(rows); } // catch the error to make it a learning experience instead of failure catch (SqlException err) { // initialize variables string errorMessages = ""; // create a readable error message from the sql exception(s) for (int i = 0; i < err.Errors.Count; i++) { errorMessages = "Index #" + i + "\n" + "Message: " + err.Errors[i].Message + "\n" + "LineNumber: " + err.Errors[i].LineNumber + "\n" + "Source: " + err.Errors[i].Source + "\n" + "Procedure: " + err.Errors[i].Procedure + "\n"; } // write the error message out to the debug console Debug.WriteLine(errorMessages); // close the connection to the database Connection1.Close(); } // return the number of rows affected return(rows); }
// insert command handler public bool DataInsert(int id, string efirst, string elast, int eid, string cfirst, string clast, int cid) { // initialize variables bool flag = false; int rowsAffected = 0; // create a connection string for the database //ConnectionStringSettings settings; //settings = System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseConnection"]; //string connectionString = settings.ConnectionString; // create a connection object //SqlConnection Connection1; //Connection1 = new SqlConnection(); //Connection1.ConnectionString = connectionString; // create db connection object SqlConnection Connection1; Connection1 = createDBConnection(); // open a connection to the table Connection1.Open(); System.Diagnostics.Debug.WriteLine("connection opened to insert"); // create a command object for the query using (SqlCommand command = new SqlCommand( "INSERT INTO Table_test VALUES(@Id,@EFirst,@ELast,@EQID,@CFirst,@CLast,@CQID)", Connection1)) { // create a parameterized query command.Parameters.Add(new SqlParameter("Id", id)); command.Parameters.Add(new SqlParameter("EFirst", efirst)); command.Parameters.Add(new SqlParameter("ELast", elast)); command.Parameters.Add(new SqlParameter("EQID", eid)); command.Parameters.Add(new SqlParameter("CFirst", cfirst)); command.Parameters.Add(new SqlParameter("CLast", clast)); command.Parameters.Add(new SqlParameter("CQID", cid)); // use try block to fail gracefully try { // execute the query rowsAffected = command.ExecuteNonQuery(); Debug.WriteLine(rowsAffected); // close the connection to the database Connection1.Close(); Debug.WriteLine("connection closed"); // set flag to true to signify data was inserted flag = true; } // catch the error to make it a learning experience instead of failure catch (SqlException err) { // initialize variables string errorMessage = ""; // create a readable error message from the sql exception(s) for (int i = 0; i < err.Errors.Count; i++) { errorMessage = "Index #" + i + "\n" + "Message: " + err.Errors[i].Message + "\n" + "LineNumber: " + err.Errors[i].LineNumber + "\n" + "Source: " + err.Errors[i].Source + "\n" + "Procedure: " + err.Errors[i].Procedure + "\n"; } // write the error message out to the debug console Debug.WriteLine(errorMessage); // close the connection to the database still and return 0 Connection1.Close(); // set flag to false to signify information was not inserted flag = false; } } // return the number of rows affected return(flag); }