2015-03-02 54 views
0

選擇查詢不起作用在asp.net c#代碼裏面Visual Studio,我開發了一個搜索按鈕。當我嘗試運行代碼時,它在編寫代碼時不會返回任何內容。我認爲問題在於stat_leger是用阿拉伯語寫的。當數據是阿拉伯語,使用動態查詢參數

SqlParameter[] para = new SqlParameter[4]; 
    para[0] = new SqlParameter("@stat_leger", ddlACCcode.SelectedValue); 
    para[1] = new SqlParameter("@branch", DDLBranch.SelectedValue); 
    para[2] = new SqlParameter("@from", db.getDate(txtFrom.Text)); 
    para[3] = new SqlParameter("@to", db.getDate(txtTo.Text)); 

DataTable dtreport = db.SelectCmdText("Select * from PostedVoucher join transactions on trans_code = stat_trans_code where stat_leger = @stat_leger and [email protected] and stat_date between @from and @to ORDER BY stat_date ", para); 

GridView1.DataSource = dtreport; 
GridView1.DataBind(); 
float GTotal = 0; 
float GTotalcrd = 0; 
float GTotaldeb = 0; 

它工作時,我試圖寫在sql服務器內相同的選擇。但與阿拉伯文我使用N它是返回記錄。

Select * from PostedVoucher join transactions on trans_code =  
    stat_trans_code 
     where stat_sub_leger = N'الصندوق' and branch= N'الفرع الرئيسي' and stat_date between '2013-12-05 00:00:00.000' AND '2013-12-05 00:00:00.000' ORDER BY stat_date ; 

我試圖把它寫在選擇但仍無法正常工作:

  DataTable dtreport = db.SelectCmdText("Select * from PostedVoucher join transactions on trans_code = stat_trans_code where stat_sub_leger = N'@stat_sub_leger' and [email protected] and stat_date between @from and @to ORDER BY stat_date ", para); 
+0

檢查字符集的字段或表的匹配。使用報價... – 2015-03-02 11:47:55

回答

0

試圖改變自己的查詢,樣品:

Select * from PostedVoucher join transactions on trans_code = stat_trans_code where stat_leger like '%' + @stat_leger + '%' OR branch like '%' + @branch + '%' AND stat_date between @from and @to ORDER BY stat_date ", para); 
0

我認爲它可能以這種方式工作:

para[0] = new SqlParameter("@stat_leger", "N'"+ddlACCcode.SelectedValue+"'") 

然後使用您的查詢自asp.net中:

DataTable dtreport = db.SelectCmdText(
    "Select * from PostedVoucher join transactions on trans_code = stat_trans_code where stat_sub_leger = N'@stat_sub_leger' and [email protected] and stat_date between @from and @to ORDER BY stat_date ", 
    para); 

然後它總是建議打印最終SQL命令來查看是否SQL命令的正確形式