时钟程序 - 时钟输入&时钟按钮 [英] Time Clock Program - Clock In & Clock Out Buttons

查看:141
本文介绍了时钟程序 - 时钟输入&时钟按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我知道我的问题是我正在开发的程序类型的一大问题。我目前正在尝试(强调尝试)为我的公司开发我的第一个C#程序,这是一个现代化的时钟程序(我们目前的程序是在10年前开发的,并且是在Access中构建的,我们正试图摆脱它) ,写入我们服务器上的SQL数据库。我有界面和数据库建立,它的功能。但它目前唯一不能做的就是将程序中的信息写入数据库。现在,界面允许用户提取他们的名字和他们的工作(这可能会改变)并按下时钟按钮,返回一个对话框,其名称,工作和当前时间作为验证。



我目前卡住的地方是使用名称(使用ComboBox和数据源设置),在表中使用PKey作为链接到employee表(因此名称必须将PKey推送到程序的编码中以备将来的步骤),作业(与名称相同的设置)并将其添加到SQL数据库中的记录中以及当前时间。



还有一个Clock Out按钮可以执行相同的操作(除了代替Add,它使用更新),它会搜索附加到名称的PKey, PKey附加到作业并使用时钟输出时间更新记录。



一直以来,Clock Out按钮都会查找一个打开Clock Out时间的记录,如果找不到,那就会出错,告诉他们他们执行了不正确的功能。此外,Clock In按钮查找员工的任何记录以及具有空白时钟输出时间的作业代码,并告诉他们程序无法创建新记录,因为那里仍有一个打开的记录。



我需要一些帮助来解决这个问题。 (我现在用C#编写了大约3个月的时间。)



这是我目前拥有的代码,它出错了:

SQLParameterCollection只接受非空的SQLParameter类型对象,而不接受字符串对象。



我相信我的sqlClkIn.Parameters.Add,我必须添加它的数据类型(nvarchar,int,bit)。这是正确的吗?



我已经发布了我的按钮代码。



Hello,

I know my issue is one of the big issues of the type of program I am developing. I am currently trying (emphasis on TRYING) to develop my first C# program for my company which is a modernized time clock program (our current one was developed over 10 years ago and was built in Access, which we are trying to get away from), that writes to a SQL database on our server. I have the interface and the database built, and it functions. But the only thing it currently cannot do is write the information from the program to the database. Right now the interface allows the users to pull up their name, and their job (this may change) and press the clock in button which returns a dialog box with their name, job and current time as verification.

Where I am currently stuck is taking the name (which is setup using a ComboBox and a Data Source), which in the table utilizes the PKey as the link to the employee table (so the name has to push the PKey into the coding of the program for future steps), the job (same setup as the name) and add it to a record in the SQL Database with the current time.

There also is a Clock Out Button that does the same thing (except instead of Add, it uses update) where it searches for the PKey attached to the name, the PKey attached to the job and updates the record with a clock out time.

All the while, the Clock Out button looks for a record with an open Clock Out time, and if it doesn't find one, it errors out, telling them that they have performed an incorrect function. In addition the Clock In Button looks for any record for the employee and the job code that has a blank clock out time and tells them that the program is incapable of creating a new record because there is still an open one out there.

I need some help in trying to figure this out. (I have been coding in C# for about 3 months now.)

This is the code that I currently have and it errors out with:
The SQLParameterCollection only accepts non-null SQLParameter type objects, not string objects.

Which I believe on my sqlClkIn.Parameters.Add, I would have to add the data type that it is (nvarchar, int, bit). Is that correct?

I have posted my button code.

<pre>        private void btnClockIn_Click(object sender, EventArgs e)
        {
            //Grab the Current Date and Time & Setup Strings for Return Message
            DateTime theDate;
            theDate = DateTime.Now;
            string strEmployee = cmbEmployee.GetItemText(cmbEmployee.SelectedItem);
            string strJob = cmbJobCode.GetItemText(cmbJobCode.SelectedItem);

            //Send the Current Date and Time to SQL Server
            SqlConnection objConnect = new SqlConnection(sqlConnect);

            string sqlClkin = "Insert Into tblTimeclock (tcEID, tcJID, tcClockIn) Values (@tcEID, @tcJID, @tcClockIn)";

            objConnect.Open();
            try
            {
                SqlCommand sqlClkIn = new SqlCommand(sqlClkin, objConnect);
                sqlClkIn.Parameters.Add("@tcEID, strEmployee");
                sqlClkIn.Parameters.Add("@tcJID, strJob");
                sqlClkIn.Parameters.Add("@tcClockIn, theDate");
                sqlClkIn.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                objConnect.Close();
            }
            //Display Message Box
            MessageBox.Show("Hello, " + strEmployee.ToString() + ".\n" + "You are clocking in at: " + theDate.ToString() + "." + "\nYou will be working job " + strJob.ToString() + ".", "Clock In", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
        }





我们将非常感谢您提供的任何帮助!



谢谢!



Any help that can be provided would be greatly appreciated!

Thanks!

推荐答案

稍微改变一下:

Change it slightly:
sqlClkIn.Parameters.AddWithValue("@tcEID", strEmployee);
sqlClkIn.Parameters.AddWithValue("@tcJID", strJob);
sqlClkIn.Parameters.AddWithValue("@tcClockIn", theDate);

SQL和框架将整理出正确的数据类型。



[edit]这将教我从OP中复制'n'粘贴 - 引用已修复! :O - OriginalGriff [/ edit]

SQL and the framework will sort out the correct datatype.

[edit]That'll teach me to copy 'n' paste from the OP - quotes fixed! :O - OriginalGriff[/edit]


制作:



Make this:

sqlClkIn.Parameters.Add("@tcEID, strEmployee");
sqlClkIn.Parameters.Add("@tcJID, strJob");
sqlClkIn.Parameters.Add("@tcClockIn, theDate");





到此:





into this:

sqlClkIn.Parameters.Add("@tcEID", strEmployee);
sqlClkIn.Parameters.Add("@tcJID", strJob);
sqlClkIn.Parameters.Add("@tcClockIn", theDate);





DbParameterCollection.Add()方法需要参数名称和值,并且您在参数名称字符串中包含了值并传递了错误参数数量。



DbParameterCollection.AddWithValue()方法也可以正常工作,并且不会使用ObsoleteAttribute进行标记。建议使用AddWithValue而不是Add now。



The DbParameterCollection.Add() method expects both a parameter name and a value, and you were including the value within the parameter name string and passing an incorrect number of parameters.

The DbParameterCollection.AddWithValue() method will also work just as well, and is not flagged with ObsoleteAttribute. It's recommended to use AddWithValue instead of Add now.


这篇关于时钟程序 - 时钟输入&amp;时钟按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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