asp.net

时间:2017-04-28 06:38:47

标签: c# html asp.net html5 sql-server-2012

public void MaxApointNo()
    {
        string db = "Data Source=DESKTOP-R6H3RTP; Initial Catalog=TokenDB; Integrated Security=true; ";
        SqlConnection mycon = new SqlConnection(db);
        mycon.Open();
        string query = "select Max(ANo + 1) AS AppointmentNO from tblApointment where ADate='"+TextBox1.Text +"' ";
        SqlCommand cmd = new SqlCommand(query, mycon);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        TextBox2.Text = ds.Tables[0].Rows[0]["AppointmentNO"].ToString();
如果可以,我是初学者帮助我  这是我在页面加载方法上调用的函数MaxApointNo   textbox2是页面加载时不会显示no的区域

3 个答案:

答案 0 :(得分:1)

我可以看到这是关于你的查询,你可以从你的查询管理它,这个代码片段将是有帮助的

    // With Custom Parameter. pass textbox value in to date parameter
    //public int GetNextAppointmentNo(DateTime date)
    public int GetNextAppointmentNo()
    {
        string db = "Data Source=DESKTOP-R6H3RTP; Initial Catalog=TokenDB; Integrated Security=true; ";

        using (SqlConnection mycon = new SqlConnection(db))
        {
            // this query is focused on Today
            string query = @"SELECT
                          COALESCE(MAX(ANo), 0) + 1 AS AppointmentNO
                        FROM tblApointment
                        WHERE CONVERT(DATE, ADate) = CONVERT(DATE, GETDATE())";

            // With Custom Parameter

            //string query = @"SELECT
            //                COALESCE(MAX(ANo), 0) + 1 AS AppointmentNO
            //            FROM tblApointment
            //            WHERE CONVERT(DATE, ADate) = CONVERT(DATE, @ADate)";

            using (SqlCommand cmd = new SqlCommand(query, mycon))
            {
                // if you need to add custom parameter to the query
                // cmd.Parameters.AddWithValue("@ADate", date);

                mycon.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    reader.Read();
                    return reader.HasRows ? Convert.ToInt32(reader["AppointmentNO"].ToString()) : 1;
                }
            }
        }
    }

无论如何尝试使用 SOLID 原则来应用更多 OOP概念,并且不使用内联查询参数,它会将您的应用程序集中到SQLInjection问题

答案 1 :(得分:0)

在页面加载时你可以这样做,

getActivity()

默认情况下,加载页面时文本框值为1

答案 2 :(得分:0)

如果您不想每次都进行回发,那么您可以按以下方式增加

  

TextBox2.Text =(Convert.ToInt32(TextBox2.Text)+ 1).ToString();