hi
I want to list all tables in sql server compact 3.5 database and total of a specific column (I have a column name "total" in all tables).when i run the code I get no error but it does not work.
can some tell me what is wrong with the code. I appreciate any help

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlServerCe;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }


        private void button2_Click(object sender, EventArgs e)
        {

        }
         private static int getTotal(string connectionString)
        {
            int total = 0;

            using (var connection = new SqlCeConnection("Data Source=|DataDirectory|Northwind.sdf;Password=***"))
            {
                connection.Open();
                var command = new SqlCeCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'Total';"
                    , connection);

                foreach (var table in from IDataRecord row in command.ExecuteReader()
                                      select new { name = row.GetValue(row.GetOrdinal("TABLE_NAME")) }
                    )
                {
                    command.CommandText = String.Format("SELECT SUM(Total) AS Total FROM {0}", table.name);
                    var tableTotal = command.ExecuteScalar();
                    total = total + (tableTotal == DBNull.Value ? 0 : (int)command.ExecuteScalar());
                }
                connection.Close();
            }
            return total;

        }
    }

}

"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'Total';" shouldn t this be--> "SELECT INFORMATION_SCHEMA.COLUMNS FROM TABLE_NAME WHERE COLUMN_NAME ='Total'"

Edited 3 Years Ago by happygeek: fixed formatting - please use CODE and/or Inline Code buttons when posting code of any kind. Thanks

This article has been dead for over six months. Start a new discussion instead.