我有一個SQL Server查詢;當我在SQL Server中運行它時,它將返回預期的結果,當我從C#代碼調用相同的查詢來填充數據集時,沒有結果。問題是什麼?在SQL Server中運行時的SQL ServerSQL查詢顯示結果時在SQL Server上執行,但從C#代碼調用時不加載結果
SELECT
t0.brandID, t0.brandName, t0.cdt, t0.udt, t0.brandstatus,
t0.AddedBy ,
(SELECT
AVG(CAST(brandID AS bigint)) FROM brands) AS brandID_AVERAGE
FROM
brands t0
WHERE
t0.cdt >= '2013-11-15 00:00:00'
AND t0.cdt <='2013-11-15 23:59:59'
GROUP BY
t0.brandID, t0.brandName, t0.cdt, t0.udt, t0.brandstatus, t0.AddedBy
和我對上面的查詢結果中運行時我的SQL查詢,如下面結果一起是:
brandID brandName cdt udt brandstatus AddedBy brandID_AVERAGE
10 khasim 2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1 1 52778
11 khasim 2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1 1 52778
12 khasim 2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1 1 52778
13 khasim 2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1 1 52778
14 khasim 2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1 1 52778
15 khasim 2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1 1 52778
16 khasim 2013-11-15 19:14:18.123 2013-11-15 19:14:18.123 1 1 52778
但是當我打電話同樣的事情,從C#與command.Parameters.AddWithValue
,我的數據集始終爲空。
我的C#代碼是
private void button1_Click(object sender, EventArgs e)
{
checkedListBox1.Items.Clear();
Cursor.Current = Cursors.WaitCursor;
string [email protected]"SELECT t0.brandID, t0.brandName, t0.cdt, t0.udt,
t0.brandstatus, t0.AddedBy,
(SELECT AVG(CAST (brandID AS bigint)) FROM brands)
AS brandID_AVERAGE FROM brands t0
WHERE t0.cdt >= @VALUE1 and t0.cdt <[email protected]
group by t0.brandID,t0.brandName,t0.cdt,t0.udt,
t0.brandstatus,t0.AddedBy";
if (textBox1.Text != string.Empty && textBox2.Text != string.Empty)
{
if (checkBox1.Checked == false)
{
try
{
if (QueryB != string.Empty)
{
using (SqlConnection con = new SqlConnection(Properties.Settings.Default.connectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand(QueryB, con);
if (checkBox6.Checked)
{
var sample1 = DateTime.ParseExact(textBox1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd") + " " + dateTimePicker2.Value.ToString("HH:mm:ss tt");
var sample2 = DateTime.ParseExact(textBox2.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd") + " " + dateTimePicker1.Value.ToString("HH:mm:ss tt");
cmd.Parameters.AddWithValue("@Value1", sample1.ToString());
cmd.Parameters.AddWithValue("@Value2", sample2.ToString());
}
else
{
if (label2.Text.Contains("datetime"))
{
cmd.Parameters.AddWithValue("@Value1", DateTime.ParseExact(textBox1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("MM-dd-yyyy") + " " + "00:00:00.000");
cmd.Parameters.AddWithValue("@Value2", DateTime.ParseExact(textBox2.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("MM-dd-yyyy") + " " + "23:59:59.999");
}
else
{
cmd.Parameters.AddWithValue("@Value1", textBox1.Text);
cmd.Parameters.AddWithValue("@Value2", textBox2.Text);
}
}
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
da.Fill(ds);
con.Close();
dataGridView1.DataSource = ds.Tables[0];
mysource = ds.Tables[0];
dataGridView1.BindingContext = new BindingContext();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
checkedListBox1.Items.Add(col.HeaderText.ToString());
}
的方式你可能需要在前端和末端每個日期的硬編碼的單引號。像t0.cdt> ='@ VALUE1' – Chuck
不要使用AddWithValue,特別是當你想傳遞一個DateTime值 – Steve
[我們可以停止使用AddWithValue()了嗎?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already /) –