2012-07-12 47 views
-1

我在寫一個檢查SQL表中是否有數據的進程。WebService檢查SQL表中是否有數據

然後我想根據是否存在做一些事情。

所以,我已經連接到數據庫的這段代碼,它正在選擇我想從表中獲取的數據。

public List<MedidasAdoptar> leerMedidasAdoptar() 
    { 

     DataSet Datos = new DataSet(); 
     string connectionString = "Initial Catalog=MyDatabase;Data Source=MyServer;Integrated Security=false;User ID=SQLUser;Password=SQLPass;"; 
     string selectCommand = 
      "SELECT [Número medida], [Cód_ Cliente], [Nombre Cliente], [Cód_ obra], [Nombre obra], Tipo, [Cód_ Medida], [Descripción Medida], [Respuesta Medida] FROM [MyDatabase$My table] " + 
      "WHERE [Cód_ Cliente] = "+codigoCliente+" AND [Cód_ obra] = "+codigoObra; 
     using (var MyConnection = new SqlConnection(connectionString)) 
     using (var MyDataAdapter = new SqlDataAdapter(selectCommand, MyConnection)) 
     { 
      MyDataAdapter.SelectCommand.CommandType = CommandType.Text; 
      MyDataAdapter.Fill(Datos); 
      MyDataAdapter.Dispose(); 
      MyConnection.Close(); 
     } 
     int i; 

     //Right here (If it is possible) I want to determine if there is some data in table 

     try 
     { 
      //...doing some stuff 
     } catch (SqlException e) 
     { 


      string msg = ""; 
      System.Console.WriteLine(msg); 
      return new List<MedidasAdoptar>(); 
     } 
+1

問題是什麼?你剛纔說過你想做點什麼,這是你使用的代碼,但是你沒有提出任何問題或問題。 – Jim 2012-07-12 16:06:42

+0

我在標題中說。我想檢查表中是否有一些數據。 – HaOx 2012-07-12 16:15:49

回答

1

這樣的事情應該工作

if (Datos.Tables[0].Rows.Count() > 0) 
+0

謝謝,它以這種方式工作: if(Datos.Tables [0] .Rows.Count> 0) – HaOx 2012-07-12 16:16:15