Advertisement
Guest User

Untitled

a guest
Mar 10th, 2013
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using System.Data;
  7. using System.Data.SqlClient;
  8.  
  9. namespace CapaAccesoDatos
  10. {
  11.     public class DALCore
  12.     {
  13.         string CONNSTR;
  14.  
  15.         public DALCore()
  16.         {
  17.             CONNSTR = "Data Source=miservidor;"
  18.                     + "Initial Catalog=mibasedadatos;"
  19.                     + "Integrated Security=False;Pooling=False;"
  20.                     + "User ID=miusuario;"
  21.                     + "Password=mipassword;";
  22.         }
  23.  
  24.         public void InsertarHIScore(string Nombre, int Score, out int? GetID)
  25.         {
  26.             try
  27.             {
  28.                 using (SqlConnection conn = new SqlConnection(CONNSTR))
  29.                 using (SqlCommand cmd = conn.CreateCommand())
  30.                 {
  31.                     conn.Open();
  32.                     cmd.CommandText = "insert into test_HIScores (" +
  33.                         " Fecha" +
  34.                         ",UserName" +
  35.                         ",Score" +
  36.                         ") values (" +
  37.                         " @fecha" +
  38.                         ",@username" +
  39.                         ",@score" +
  40.                         ")";
  41.  
  42.                     cmd.Parameters.Add("@fecha", SqlDbType.DateTime).Value = DateTime.Now;
  43.                     cmd.Parameters.Add("@username", SqlDbType.NVarChar, 50).Value = Nombre;
  44.                     cmd.Parameters.Add("@score", SqlDbType.Int).Value = Score;
  45.                     cmd.ExecuteNonQuery();
  46.  
  47.                     cmd.Parameters.Clear();
  48.                     cmd.CommandText = "select IDENT_CURRENT('[dbo].[test_HIScores]')";
  49.  
  50.                     var tmp = cmd.ExecuteScalar();
  51.                     if (tmp != null)
  52.                     {
  53.                         int p;
  54.                         bool b = int.TryParse(Convert.ToString(tmp), out p);
  55.                         if (b == true)
  56.                             GetID = p;
  57.                         else
  58.                             GetID = null;
  59.                     }
  60.                     else
  61.                         GetID = null;
  62.  
  63.                 }
  64.             }
  65.             catch (Exception ex)
  66.             {
  67.                 throw new System.Exception(ex.Message);
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement