C# 我想限制用户每天只插入一次Performance

C# 我想限制用户每天只插入一次Performance,c#,sql-server,restriction,C#,Sql Server,Restriction,我写了这段代码,但它允许用户每天输入性能记录不止一次,而我希望用户每天只能输入一次 起初,我编写了如下条件:if(dt.Rows.Count

我写了这段代码,但它允许用户每天输入性能记录不止一次,而我希望用户每天只能输入一次

起初,我编写了如下条件:
if(dt.Rows.Count<0)
,但在这种情况下,只有else部分工作,用户不能至少输入一次记录

 protected void subtbtn_Click(object sender, EventArgs e)

    {
        DataTable dt = new DataTable();

        SqlConnection connection = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=incible;Integrated Security=true");

        connection.Open();

        string sqlStatement = "SELECT * FROM prfrmnce where u_name='" + unamelbl.Text + "' and datetime ='" + Calendar1.TodaysDate + "'";

        SqlCommand cmd1 = new SqlCommand(sqlStatement, connection);

        SqlDataAdapter sqlDa = new SqlDataAdapter(cmd1);

         sqlDa.Fill(dt);

         if (dt.Rows.Count == 0)

         {
             // Open the database connection

             string myConnectionString = @"Data Source=.\sqlexpress;Initial Catalog=incible;Integrated Security=true";

             SqlConnection con = new SqlConnection(myConnectionString);
             con.Open();
             //Query to insert images name and Description into database
             SqlCommand cmd = new SqlCommand("Insert into prfrmnce(u_name,designatn,datetime,todaytask,tmrwplan) values(@uname,@dsgntn,@date,@twrk,@tmrwpln)  ", con);
             //Passing parameters to query
             cmd.Parameters.AddWithValue("@uname", UserName.Text);
             cmd.Parameters.AddWithValue("@dsgntn", desigtxtbx.Text);
             cmd.Parameters.AddWithValue("@twrk", tfortday.Text);
             cmd.Parameters.AddWithValue("@tmrwpln", pfortmrw.Text);
             cmd.Parameters.AddWithValue("@date", Calendar1.TodaysDate);

             cmd.ExecuteNonQuery();
             resultlebel.Text = "data added successfully";

             //Close dbconnection


             con.Close();
             tfortday.Text = string.Empty;
             pfortmrw.Text = string.Empty;
             GridView1.DataBind();
         }
         else if (dt.Rows.Count > 0) 
         {
             errorlabel.Text = "You have already submitted your today's performance";
         }


    }

我猜这是一个
DateTime
问题。DateTime不是一天,它也有小时、分钟等

您应该只使用日期的日期部分

所以在你的提问中

改变

and datetime ='" + Calendar1.TodaysDate + "'"

顺便说一下,您应该使用参数,而不是字符串连接。
请参见此查询是否正确

string sqlStatement = "SELECT * FROM prfrmnce where u_name='" + unamelbl.Text + "' and datetime ='" + Calendar1.TodaysDate + "'";

问题可能是Calendar1。TodaysDate提供日期和时间,因此您将始终获得dt.Rows.Count作为0

错误标签的内容为“提交了您今天的数据”。应该是“今天提交的”我不是来纠正语法的你是来寻求帮助的,而且是免费的。如果有人愿意花时间提出建议,你至少可以优雅地接受。但我的实际问题呢?我改变了它,但问题没有解决。。就连我也在使用参数。。即使if条件为false,也会执行if条件的语句。。
string sqlStatement = "SELECT * FROM prfrmnce where u_name='" + unamelbl.Text + "' and datetime ='" + Calendar1.TodaysDate + "'";