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;
}
}
}