将datetimepicker和datagridview数据保存到db中 [英] saving datetimepicker and datagridview data into db

查看:94
本文介绍了将datetimepicker和datagridview数据保存到db中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单包含一个dateTimePicker和一个datagridview,我想将数据保存到sql db

我正在使用下面的代码,但保持给出错误。错误消息将varchar数据类型转换为日期时间数据类型导致超出范围值

请帮帮我



  for  int  i =  0 ; i <  dgvSales.Rows.Count -1; i ++)
{
;
string StrQuery = null ;
StrQuery = @ INSERT INTO tblstock VALUES(' + dgvSales.Rows [i] .Cells [ 产品ID]。值+ ',' + this .dtpStockdate.Value + ',' + dgvSales.Rows [i] .Cells [ 产品名称]。值+ ',' + dgvSales.Rows [i] .Cells [ Quantity]。值+ ');
comm.CommandText = StrQuery;
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}

解决方案

在不知道确切错误信息的情况下......很难帮助你。



我猜你得到的是数据类型不匹配类型错误。我猜你的表中的数量字段是数字的,你用单引号包围它。



你真的应该考虑使用参数。如果用户能够手动输入网格,它将有助于解决这类问题并阻止SQL注入。


探索这些并尝试自己..

如何将日期插入sql db date使用dateTimePicker的列? [ ^ ]

http://stackoverflow.com/questions/3147258/insert-datetimepicker-text-in-sql-database [ ^ ]

http://stackoverflow.com/questions / 12776618 / read-data-from-datagridview-and-save-to-database [ ^ ]

http://stackoverflow.com/questions/3723646/how-to-save-data-from-a-datagridview- in-database-c-sharp-winforms [ ^ ]


在你的StrQuery变量中,尝试替换它....

  .dtpStockdate.Value 



这个.. 。

  this  .dtpStockdate.Value.ToShortDateString(); 


my form contains one dateTimePicker and a datagridview and i want to save data into sql db
i am using the codes below but keeps given an error. the error msg "conversion of a varchar data type to a datetime data type resulted in an out of range value"
please help me out

for (int i = 0; i < dgvSales.Rows.Count -1; i++)
                        {
                            ;
                            string StrQuery = null;
                            StrQuery = @"INSERT INTO tblstock VALUES ('" + dgvSales.Rows[i].Cells["Product ID"].Value + "', '" + this.dtpStockdate.Value + "', '" + dgvSales.Rows[i].Cells["Product Name"].Value + "', '" + dgvSales.Rows[i].Cells["Quantity"].Value + "')";
                            comm.CommandText = StrQuery;
                            conn.Open();
                            comm.ExecuteNonQuery();
                            conn.Close();
                        }

解决方案

Without knowing the exact error message...it's hard to help you.

My guess it that you're getting a data type mismatch type error. I would guess that the Quantity field in your table is numeric and you are passing it in surrounded by single quotes.

You really should look into using parameters. It will help with problems like this and will prevent SQL injection if users are able to manually type into the grid.


Explore these and then try yourself..
How to INSERT date into sql db date column using dateTimePicker?[^]
http://stackoverflow.com/questions/3147258/insert-datetimepicker-text-in-sql-database[^]
http://stackoverflow.com/questions/12776618/read-data-from-datagridview-and-save-to-database[^]
http://stackoverflow.com/questions/3723646/how-to-save-data-from-a-datagridview-into-database-c-sharp-winforms[^]


In your StrQuery variable, try replacing this....

this.dtpStockdate.Value


with this...

this.dtpStockdate.Value.ToShortDateString();


这篇关于将datetimepicker和datagridview数据保存到db中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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