Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var con = new SqlConnection("Data Source=local;Initial Catalog=Test;Integrated Security=True");
- using (con)
- using (var command = new SqlCommand("SELECT col1,col2 FROM some table", con))
- {
- con.Open();
- command.ExecuteNonQuery();
- }
- public class ClassName
- {
- public string Col1 { get; set; }
- public int Col2 { get; set; }
- }
- ClassName[] allRecords = null;
- string sql = @"SELECT col1,col2
- FROM some table";
- using (var command = new SqlCommand(sql, con))
- {
- con.Open();
- using (var reader = command.ExecuteReader())
- {
- var list = new List<ClassName>();
- while (reader.Read())
- list.Add(new ClassName { Col1 = reader.GetString(0), Col2 = reader.GetInt32(1) });
- allRecords = list.ToArray();
- }
- }
- DataTable dt = new DataTable();
- using (var con = new SqlConnection("Data Source=local;Initial Catalog=Test;Integrated Security=True"))
- {
- using (var command = new SqlCommand("SELECT col1,col2" +
- {
- con.Open();
- using (SqlDataReader dr = command.ExecuteReader())
- {
- dt.Load(dr);
- }
- }
- }
- SqlDataAdapter da = new SqlDataAdapter(command);
- da.Fill(dt);
- foreach (DataRow dr in dt.Rows)
- {
- if (dr.Field<string>("col1") == "yourvalue") //your condition
- {
- }
- }
- try
- {
- SqlCommand comm = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories;",connection);
- connection.Open();
- SqlDataReader reader = comm.ExecuteReader();
- List<string> str = new List<string>();
- int i=0;
- while (reader.Read())
- {
- str.Add( reader.GetValue(0).ToString() );
- }
- reader.Close();
- }
- catch (Exception)
- {
- throw;
- }
- finally
- {
- connection.Close();
- }
- public void PrintSql_Array()
- {
- int[] numbers = new int[4];
- string[] names = new string[4];
- string[] secondNames = new string[4];
- int[] ages = new int[4];
- int cont = 0;
- string cs = @"Server=ADMINSQLEXPRESS; Database=dbYourBase; User id=sa; password=youpass";
- using (SqlConnection con = new SqlConnection(cs))
- {
- using (SqlCommand cmd = new SqlCommand())
- {
- cmd.Connection = con;
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = "SELECT * FROM tbl_Datos";
- con.Open();
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- da.Fill(dt);
- foreach (DataRow row in dt.Rows)
- {
- numbers[cont] = row.Field<int>(0);
- names[cont] = row.Field<string>(1);
- secondNames[cont] = row.Field<string>(2);
- ages[cont] = row.Field<int>(3);
- cont++;
- }
- for (int i = 0; i < numbers.Length; i++)
- {
- Console.WriteLine("{0} | {1} {2} {3}", numbers[i], names[i], secondNames[i], ages[i]);
- }
- con.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement