16 Ocak 2016 Cumartesi

c# SQL Insert/Update/Select/Delete

  1. //Basic SELECT method to populate a DataSet from a SqlDataAdapter
  2. SqlConnection sqlConn = new SqlConnection(connection string here);
  3. SqlDataAdapter sqlAdapt = new SqlDataAdapter(@"SELECT * FROM tableName WHERE conditionColumn='False'", sqlConn);
  4. SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(sqlAdapt);
  5. DataSet sqlSet = new DataSet();
  6. feedbackAdapt.Fill(sqlSet, "dataSetTableName");
  7. feedbackConn.Close();
  8. //Basic INSERT method with Parameters
  9. SqlConnection sqlConn = new SqlConnection(connection string here);
  10. SqlCommand sqlComm = new SqlCommand();
  11. sqlComm = sqlConn.CreateCommand();
  12. sqlComm.CommandText = @"INSERT INTO tableName (paramColum) VALUES (@paramName)";
  13. sqlComm.Parameters.Add("@paramName", SqlDbType.VarChar);
  14. sqlComm.Parameters["@paramName"].Value = paramSource;
  15. sqlConn.Open();
  16. sqlComm.ExecuteNonQuery();
  17. sqlConn.Close();
  18. //Basic UPDATE method with Parameters
  19. SqlConnection sqlConn = new SqlConnection(connection string here);
  20. SqlCommand sqlComm = new SqlCommand();
  21. sqlComm = sqlConn.CreateCommand();
  22. sqlComm.CommandText = @"UPDATE tableName SET paramColumn='@paramName' WHERE conditionColumn='@conditionName'";
  23. sqlComm.Parameters.Add("@paramName", SqlDbType.VarChar);
  24. sqlComm.Parameters["@paramName"].Value = paramSource;
  25. sqlComm.Parameters.Add("@conditionName", SqlDbType.VarChar);
  26. sqlComm.Parameters["@conditionName"].Value = conditionSource;
  27. sqlConn.Open();
  28. sqlComm.ExecuteNonQuery();
  29. sqlConn.Close();
  30. //Basic DELETE method with Parameters
  31. SqlConnection sqlConn = new SqlConnection(connection string here);
  32. SqlCommand sqlComm = new SqlCommand();
  33. sqlComm = sqlConn.CreateCommand();
  34. sqlComm.CommandText = @"DELETE FROM tableName WHERE conditionColumn='@conditionName'";
  35. sqlComm.Parameters.Add("@conditionName", SqlDbType.VarChar);
  36. sqlComm.Parameters["@conditionName"].Value = conditionSource;
  37. sqlConn.Open();
  38. sqlComm.ExecuteNonQuery();
  39. sqlConn.Close();