1 / 6

Database tilgang

Database tilgang. Opret forbindelse til din Ms SQL database inde i Visual Studio fra Server Explorer vinduet. Connection til MsSql-database. System.Data.IDbConnection con = new System.Data.SqlClient.SqlConnection();

milly
Download Presentation

Database tilgang

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Database tilgang Opret forbindelse til din Ms SQL database inde i Visual Studio fra Server Explorer vinduet.

  2. Connection til MsSql-database System.Data.IDbConnection con = new System.Data.SqlClient.SqlConnection(); con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MinDatabase.mdf;Integrated Security=True;User Instance=True"; Bemærk at din connectionstring er specifik for din konkrete database. I stedet for det fysiske mappe navn kan man bruge |DataDirectory| hvilket angiver default data-mappen – for web er det App_Data og ellers typisk mappen med exe-programmet.

  3. Læsning af tabel med DataReader System.Data.IDbCommand cmd = con.CreateCommand(); cmd.CommandText = "SELECT * FROM Forening"; System.Data.IDataReader dataReader = cmd.ExecuteReader(); while (dataReader.Read()) { int tmp_id = int.Parse(dataReader["ID"].ToString()); string tmp_navn = dataReader["Navn"].ToString(); ………………………. ………………………. } dataReader.Close(); /* luk forespørsel*/ con.Close();

  4. Opret ny entitet (række) i tabel Datavariable: string navn System.Data.IDbCommand cmd = con.CreateCommand(); int antalIndsat; cmd.CommandText = "INSERT INTO Forening ” + " (navn)” // id ved autonummerering + " VALUES” + " ('"+navn+"')"; // navn er en variabel antalIndsat = cmd.ExecuteNonQuery(); /* udfør SQL Update/Insert/Delete */ // hent tildelt autonummererede nøgle cmd.CommandText = "select @@identity as lastInsertedKey"; int lastInsertedKey = int.Parse(cmd.ExecuteScalar().ToString()); con.Close();

  5. Opdater entitet (række) i tabel Datavariable: string navn og int id System.Data.IDbCommand cmd = con.CreateCommand(); int antalRettet; cmd.CommandText = "UPDATE Forening SET” +" Navn='"+navn+"'” // navn er en variabel +" WHERE ID="+id; // id er en variabel og nøgle antalRettet = cmd.ExecuteNonQuery(); /* udfør SQL Update/Insert/Delete */ con.Close();

  6. Slet entitet (række) i tabel Datavariable: int id System.Data.IDbCommand cmd = con.CreateCommand(); int antalSlettede; cmd.CommandText = "DELETE FROM Forening" + " WHERE ID=" + id; // id er en variabel og nøgle antalSlettede = cmd.ExecuteNonQuery(); /* udfør SQL Update/Insert/Delete */ con.Close();

More Related