开发者

InvalidCastException in Npgsql

InvalidCastException in Npgsql

My question is:I want to write the result by suming a column which is money type between two dates.

Code:

using (NpgsqlConnection b = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=postgres;Password=xxxxxxxx;DataBase=deneme;"))
        {
            try
            {
                b.Open();
                NpgsqlCommand c = new NpgsqlCommand("SELECT  SUM(tutar) FROM market where tarih between '" + dateTimePicker1.Value + "' and '" + date开发者_StackOverflowTimePicker2.Value + "'", b);
                double toplam = ((double)c.ExecuteScalar());
                b.Close();
                b.Dispose();
                MessageBox.Show(toplam.ToString("n"));
            }


Try casting to decimal instead of double.

The Postgres documentation for the "money" type suggests that the output is in the form "$1,000.00" (locale-dependent), in which case you'd need to parse the return value and remove the punctuation before casting.

Also, if that's production code, you'll want to parameterize your SQL instead of appending parameter values in there, otherwise you're open to potential SQL injection attacks and may have performance problems as well.

There are a few threads discussing currency data types in .NET which might also be helpful. Here's one: Does anyone know of a money type in .NET?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜