在c#的帮助下在sql中添加数据 [英] about add data in sql with the help of c#

查看:73
本文介绍了在c#的帮助下在sql中添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

aoa ...在我的应用程序支付滚动模块我想在sql server中添加....借助于数月数据库添加...

aoa... in my app Pay roll module i want to add in sql server.... with the help of months data cab be added...

actually i want to add data in sql server for the employees... i want to avoid last three entries for the same month... which query can be made to insert data into table and on the same month same employee can not be add?



我的样本数据如下:




as my sample data is given below

Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	  April	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	  May	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	  June	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	  July	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	 August	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	 September	2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	October	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	October	        2014
Miss.Sumera	Assistant Manager  78.00	96.00	14.00	2403.00	October	        2014



和我的c#代码是:




and my c# code is:

private void button3_Click(object sender, EventArgs e)
       {
           SqlCommand cmd = sqlconn.CreateCommand();
           cmd.CommandText = "insert into payroll values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox8.Text + "','" + textBox7.Text + "','" + textBox6.Text + "','" + textBox10.Text + "','" + textBox9.Text + "','" + textBox12.Text + "','"+comboBox1.Text+"','"+comboBox2.Text+"')";
           try
           {
               cmd.ExecuteNonQuery();
           }
           catch (SqlException err)
           {
               MessageBox.Show(err.Message);
           }
           MessageBox.Show("Record Entered Successfull");


       }

推荐答案

实际上我想在sql server中添加数据对于员工......我想避免同一个月的最后三个条目...可以进行哪些查询以将数据插入表中,同一个月同一个员工无法添加?



为此,您需要先检查:

"actually i want to add data in sql server for the employees... i want to avoid last three entries for the same month... which query can be made to insert data into table and on the same month same employee can not be add?"

To do that, you need to check first:
SELECT COUNT(*) FROM MyTable
GROUP BY UserName, Year, Month 
WHERE Year = @CurrentYear AND Month = @CurrentMonth

(显然,你必须提供年和月价值

那么如果你可以插入少于三个的数量



我会在存储过程中这样做(和我将月份和年份存储为DateTime,而不是单独的字段)

(Obviously, you'll have to provide the year and month values
Then if the number of less than three you can insert

I'd do this in a stored procedure (and I'd store the month and year as a DateTime, not separate fields)


这篇关于在c#的帮助下在sql中添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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