IIRLaborDBClientProg

A VIK Wikiből
Ugrás a navigációhoz Ugrás a kereséshez

Ez az oldal a korábbi SCH wiki-ről lett áthozva. Az eredeti változata itt érhető el.

Ha úgy érzed, hogy bármilyen formázási vagy tartalmi probléma van vele, akkor kérlek javíts rajta egy rövid szerkesztéssel.

Ha nem tudod, hogyan indulj el, olvasd el a migrálási útmutatót


Kapcsolódó tárgyak, tanfolyamok

Lekérdezés futtatása

		  private void Pelda1_Click(object sender, EventArgs e)
		  {
				string connectionStr = "Data Source=LABORVPC;Initial Catalog=conf;Integrated Security=True";
				SqlConnection conn = new SqlConnection(connectionStr);

				string commandStr = "SELECT * FROM Author";

				SqlCommand cmd = new SqlCommand(commandStr, conn);

				conn.Open();

				SqlDataReader rdr = cmd.ExecuteReader();
				while (rdr.Read())
				{
					 txtOutput.Text += "\r\n " + rdr["author_name"].ToString();
					 
				}
				rdr.Close();
				conn.Close();
		  }

Új rekord beszúrása

		  private void Pelda2_Click(object sender, EventArgs e)
		  {
				string connectionStr = "Data Source=LABORVPC;Initial Catalog=conf;Integrated Security=True";
				SqlConnection conn = new SqlConnection(connectionStr);

				string commandStr = "INSERT into Author VALUES (6, 'author6')";

				SqlCommand cmd = new SqlCommand(commandStr, conn);

				conn.Open();

				cmd.ExecuteNonQuery();

				conn.Close();
		  }

Tárolt eljárás hívása

		  private void Pelda3_Click(object sender, EventArgs e)
		  {
				string connectionStr = "Data Source=LABORVPC;Initial Catalog=conf;Integrated Security=True";
				SqlConnection conn = new SqlConnection(connectionStr);

				string commandStr = "NewAuthor";

				SqlCommand cmd = new SqlCommand(commandStr, conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.Add("@id", SqlDbType.Int);
				cmd.Parameters.Add("@name", SqlDbType.NVarChar);

				cmd.Parameters["@id"].Value = 7;
				cmd.Parameters["@name"].Value = "author7";

				conn.Open();

				cmd.ExecuteNonQuery();

				conn.Close();
		  }


Offline adatelérés DataSet és DataTable segítségével

		  private void pelda4Button_Click(object sender, EventArgs e)
		  {
				string connectionStr = "Data Source=LABORVPC;Initial Catalog=conf;Integrated Security=True";
				SqlConnection conn = new SqlConnection(connectionStr);

				string cmdStr = "SELECT * FROM Author";

				SqlDataAdapter adapter = new SqlDataAdapter(cmdStr, conn);

				DataSet confDS = new DataSet("Conf");

				adapter.Fill(confDS);

				DataTable authorTable = confDS.Tables[0];

				foreach (DataRow authorRow in authorTable.Rows)
				{
					 txtOutput.Text += "\r\n " + authorRow["author_name"].ToString();
				}
		  }

Típusos DataSet

		  private void pelda5Button_Click(object sender, EventArgs e)
		  {
				ConfManager.conf.AuthorDataTable authorTable =
					 new conf.AuthorDataTable();
				AuthorTableAdapter authorAdapter = new AuthorTableAdapter();
				authorAdapter.Fill(authorTable);

				for (int i = 0; i < authorTable.Count; i++)
				{
					 ConfManager.conf.AuthorRow authorRow = authorTable[i];
					 txtOutput.Text += "\r\n " + authorRow.author_name;
				}
		  }

Módosítás visszavezetése az adatbázisba

		  private void pelda6Button_Click(object sender, EventArgs e)
		  {
				ConfManager.conf.AuthorDataTable authorTable =
					 new conf.AuthorDataTable();
				AuthorTableAdapter authorAdapter = new AuthorTableAdapter();
				authorAdapter.Fill(authorTable);

				authorTable[3].author_name += " " + authorTable[3].author_name;
				authorAdapter.Update(authorTable);
		  }
	 }

-- palacsint - 2007.12.07.