帮助处理日期和数据库 [英] Helping in working with dates and database

查看:54
本文介绍了帮助处理日期和数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正试着弄清楚这段代码的用处.代码需要从Winform(C#)获取一些信息,并检查广告牌在fromDate到toDate之间是否可用(如果可用),将订单插入DB中(如果没有),以抛出消息"not available".但是编译器会在?"附近抛出消息语法错误"

Hello, im tryin to figure what worng with this code. the code need to get some information from Winform (C#), and to check if the billboard are available between fromDate to toDate if available insert the order into the DB if not, to throw message "not available". but the compiler throw message "wrong syntax near '?'"

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

namespace forum
{
    public partial class addNewOrder : Form
    {
        public addNewOrder()
        {
            InitializeComponent();
        }

        private void addNewOrder_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'forumArtDBDataSet3.agents' table. You can move, or remove it, as needed.
            this.agentsTableAdapter.Fill(this.forumArtDBDataSet3.agents);
            // TODO: This line of code loads data into the 'forumArtDBDataSet2.customers' table. You can move, or remove it, as needed.
            this.customersTableAdapter.Fill(this.forumArtDBDataSet2.customers);
            // TODO: This line of code loads data into the 'forumArtDBDataSet1.billBoards' table. You can move, or remove it, as needed.
            this.billBoardsTableAdapter.Fill(this.forumArtDBDataSet1.billBoards);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=myServer");
            var cmdCheck = new SqlCommand("SELECT Count(*) FROM Orders WHERE billNumber = ? AND (fromDate BETWEEN ? and ? OR toDate BETWEEN ? AND ?)", conn);
            cmdCheck.Parameters.Add(new SqlParameter("@p1", SqlDbType.VarChar)).Value = billNumber.Text;
            cmdCheck.Parameters.Add(new SqlParameter("@p2", SqlDbType.DateTime)).Value = fromDate.Value;
            cmdCheck.Parameters.Add(new SqlParameter("@p3", SqlDbType.DateTime)).Value = toDate.Value;
            cmdCheck.Parameters.Add(new SqlParameter("@p4", SqlDbType.DateTime)).Value = fromDate.Value;
            cmdCheck.Parameters.Add(new SqlParameter("@p5", SqlDbType.DateTime)).Value = toDate.Value;
            try
            {
                conn.Open();
                int Result = Convert.ToInt32(cmdCheck.ExecuteScalar());
                if (Result > 0)
                {
                    MessageBox.Show("השלט אינו פנוי בתאריך המבוקש");
                }
                else
                {
                    SqlCommand inData = new SqlCommand("INSERT INTO orders (billNumber,companyName,fromDate,toDate,fullName) VALUES ('" + billNumber.Text + "','" + companyName.Text + "','" + fromDate.Text + "','" + toDate.Text + "','" + fullName.Text + "')", conn);
                    conn.Open();
                    inData.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("השלט התווסף בהצלחה");
                    Close();
                }
            }
            catch (SqlException ex)
            {

                MessageBox.Show(ex.Message + "\n\n" + ex.InnerException);
            }
            finally
            {
                conn.Close();
            }
        }
    }
}

推荐答案

使用@代替?. @ p1 @ p2.
?是无效的语法,请在查询中使用参数名称.
希望这会有所帮助.
use @ instead of ?. @p1 @p2.
? is invalid syntax, Use parameter names in your query.
Hope this helps.


这篇关于帮助处理日期和数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆