Note that there are some explanatory texts on larger screens.

plurals
  1. PO(C# WPF SQL Server) Is it possible to connect to the database several times in one function?
    text
    copied!<p>Is it possible to connect to the database several times in one function?</p> <p>In if <code>SqlCommand</code> does not fill table.</p> <p>Piece of code:</p> <pre><code> public void region_wypelnij() { string Region = RegionTextBox.Text; string Kraj = ((DataRowView)KrajComboBox.SelectedItem).Row.ItemArray[0].ToString(); String tresc = "Data source=ADAM_LAPTOP; Integrated Security=true; Database=tmargacz"; SqlConnection conn = new SqlConnection(tresc); SqlCommand polecenie = new SqlCommand("Select IdRegionu From Region Where Nazwa='" + Region + "'", conn); SqlDataAdapter adapter = new SqlDataAdapter(polecenie); DataSet ds = new DataSet(); //reprezentują całą bazę danych wraz z relacjami pomiędzy tabelami adapter.Fill(ds, "Region"); int licznik = ds.Tables["Region"].Rows.Count; MessageBox.Show(licznik.ToString()); if (licznik &gt; 0) { string index = ds.Tables["Region"].Rows[0]["IdRegionu"].ToString(); int indeks = Convert.ToInt32(index); String tresc2 = "Data source=ADAM_LAPTOP; Integrated Security=true; Database=tmargacz"; SqlConnection conn2 = new SqlConnection(tresc2); SqlCommand polecenie2 = new SqlCommand("INSERT INTO Region VALUES ((SELECT Nazwa FROM Region WHERE IdRegionu ='" + indeks + "'), (SELECT IdKraju FROM Kraj WHERE Nazwa ='" + Kraj + "'))", conn2); SqlDataAdapter adapter2 = new SqlDataAdapter(polecenie2); DataSet ds2 = new DataSet(); //reprezentują całą bazę danych wraz z relacjami pomiędzy tabelami adapter.Fill(ds2, "Region"); } else { String tresc2 = "Data source=ADAM_LAPTOP; Integrated Security=true; Database=tmargacz"; SqlConnection conn2 = new SqlConnection(tresc2); SqlCommand polecenie2 = new SqlCommand("INSERT INTO Region VALUES ('" + Region + "', (SELECT IdKraju FROM Kraj WHERE Nazwa ='" + Kraj + "'))", conn2); SqlDataAdapter adapter2 = new SqlDataAdapter(polecenie2); DataSet ds2 = new DataSet(); //reprezentują całą bazę danych wraz z relacjami pomiędzy tabelami adapter.Fill(ds2, "Region"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload