将值插入SQL Server表并单击空白记录 [英] Insert values into the SQL server table and click on the blank records

查看:76
本文介绍了将值插入SQL Server表并单击空白记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据插入sqlserver数据表。我有很多参数。如果我填写值并单击插入按钮,则值正在插入并正确进入表中。但如果我只插入几个值意味着不是全部。所以它给了我一个错误。

i想要点击按钮没有任何价值所以它不应该给我一个错误。

请指导我如何解决这个问题我的这些代码。



我尝试过:



 SqlConnection con = new SqlConnection(Data Source = .; Initial Catalog = DelhiEMP; Integrated Security = True); 
con.Open();
string query =insert into itemmaster(itemcode,name,salesprice,salestax,profit,quantityonhand)值(@itemcode,@ name,@ salesprice,@ profit,@ salestax,@ quantityonhand);
SqlCommand cmd = new SqlCommand(query,con);


cmd.Parameters.AddWithValue(@ itemcode,textBox1.Text);
cmd.Parameters.AddWithValue(@ name,textBox2.Text);
cmd.Parameters.AddWithValue(@ salesprice,textBox3.Text);
cmd.Parameters.AddWithValue(@ salestax,textBox4.Text);
cmd.Parameters.AddWithValue(@ profit,textBox5.Text);
cmd.Parameters.AddWithValue(@ quantityonhand,textBox6.Text);
cmd.ExecuteNonQuery();
MessageBox.Show(已保存);
con.Close();

解决方案

首先,如果所有列都为null或者有任何默认值,则需要检查数据库表模式指定的值只有你的目标实现,没有输入任何东西并点击按钮你的空白记录被创建。



这里你使用 itemmaster 表,所以如果它的schmea允许空值/默认值到列 itemcode,name,salesprice,salestax,profit,quantityonhand 那么你可以在表中插入一个默认值/ null值记录。



你可以分享你的表架构,也不要忘记更新你的错误细节,比如什么插入空行时收到的错误消息。





更新



正如您按照以下脚本共享数据库表模式



  CREATE   TABLE  [dbo]。[itemmaster](
[itemcode] [ int ] NOT NULL
[name] [ varchar ] ( 50 NULL
[salesprice] [十进制]( 18 0 NULL
[salestax] [十进制]( 18 0 NULL
[profit] [ decimal ]( 18 0 NULL
[quantityonhand] [ decimal ]( 18 0 NU LL
PRIMARY KEY CLUSTERED

[itemcode] ASC
WITH (PAD_INDEX = OFF ,STATISTICS_NORECOMPUTE = OFF ,IGNORE_DUP_KEY = OFF ,ALLOW_ROW_LOCKS = ON ,ALLOW_PAGE_LOCKS = ON ON [ PRIMARY ]
ON [ PRIMARY ]





您已设置 itemcode 作为具有out标识列的主键,因此您需要在插入记录时为该列提供值。其余列允许为null,因此您可以为其指定值,以便将数据插入 itemmaster 表。



或者根据下面的脚本,您可以修改表模式并将 itemcode 定义为带主键的标识列,然后数据库将自动增加下一个身份值并在插入记录时将其插入到您的表中。



  / *   *****对象:表[dbo]。[itemmaster]脚本日期:5/1/2017 8:08:10 PM ****** /  
DROP [dbo]。[itemmaster]
GO

/ * *** **对象:表[dbo]。[itemmaster]脚本日期:5/1/2017 8:08:10 PM ****** /
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
< span class =code-keyword> GO

SET ANSI_PADDING ON
GO

创建 [dbo]。[itemmaster](
[itemcode] [ int ] IDENTITY 1 1 NOT NULL
[name] [ varchar ]( 50 NULL
[salesprice] [ 十进制]( 18 0 NULL
[salestax] [十进制]( 18 ,< span class =code-digit> 0 ) NULL
[profit] [ decimal ]( 18 0 NULL
[quantityonhand] [ decimal ]( 18 0 NULL
CONSTRAINT [PK__itemmast__986EE407B02D1FB9] < span class =code-keyword> PRIMARY KEY CLUSTERED

[itemcode] ASC
WITH (PAD_INDEX = OFF ,STATISTICS_NORECOMPUTE = OFF ,IGNORE_DUP_KEY = OFF ,ALLOW_ROW_LOCKS = ON ,ALLOW_PAGE_LOCKS = ON ON [ PRIMARY ]
ON [ PRIMARY ]

GO

SET ANSI_PADDING OFF
GO







建议:请不要在您的应用程序中使用内联查询,它将允许 SQL注入 [ ^ ]将被注入您的应用程序。

In使用内联查询在您的应用程序中使用存储过程,如存储过程演示 [ ^ ]


i am inserting data into the sqlserver data table. and i have many parameters. if i fill the values and click on the insert button so values are inserting and going into the table properly. but if i am inserting only few values means not all. so it gives me an error.
i want if i click on the button without any value so it should not give me an error.
please guide me how to solve this condition with my these codes.

What I have tried:

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DelhiEMP;Integrated Security=True");
           con.Open();
           string query = "insert into itemmaster(itemcode, name, salesprice, salestax,profit, quantityonhand) values(@itemcode, @name, @salesprice,@profit,@salestax,  @quantityonhand)";
           SqlCommand cmd = new SqlCommand(query, con);


           cmd.Parameters.AddWithValue("@itemcode", textBox1.Text);
           cmd.Parameters.AddWithValue("@name", textBox2.Text);
           cmd.Parameters.AddWithValue("@salesprice", textBox3.Text);
           cmd.Parameters.AddWithValue("@salestax", textBox4.Text);
           cmd.Parameters.AddWithValue("@profit", textBox5.Text);
           cmd.Parameters.AddWithValue("@quantityonhand", textBox6.Text);
           cmd.ExecuteNonQuery();
           MessageBox.Show("Saved");
           con.Close();

解决方案

First of all you need to check your database table schema, if all the columns are null or having any default value specified then only your goal is achieved, means with out entering anything and clicking on button your blank record is created.

here you are using itemmaster table, so if its schmea is allowing null value/ default value to columns itemcode, name, salesprice, salestax,profit, quantityonhand then you can insert a default value/null value record to your table.

can you please share your table schema, and also don't forget to updated your error details, like what error message you are getting while inserting a blank row.


UPDATES

As you have shared your database table schema as per below script

CREATE TABLE [dbo].[itemmaster](
[itemcode] [int] NOT NULL,
[name] [varchar](50) NULL,
[salesprice] [decimal](18, 0) NULL,
[salestax] [decimal](18, 0) NULL,
[profit] [decimal](18, 0) NULL,
[quantityonhand] [decimal](18, 0) NULL,
PRIMARY KEY CLUSTERED 
(
[itemcode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]



You have set itemcode as primary key with out identity column, so you need to provide value for that column while inserting record. Rest of the columns are allowed null, so you can either specified value for it or not that work to insert the data to your itemmaster table.

Or As per the below script you can modify your table schema and define itemcode as Identity Column with Primary Key, then database will automatically increment the next identity value and insert it to your table while inserting the records.

/****** Object:  Table [dbo].[itemmaster]    Script Date: 5/1/2017 8:08:10 PM ******/
DROP TABLE [dbo].[itemmaster]
GO

/****** Object:  Table [dbo].[itemmaster]    Script Date: 5/1/2017 8:08:10 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[itemmaster](
	[itemcode] [int] IDENTITY(1,1) NOT NULL,
	[name] [varchar](50) NULL,
	[salesprice] [decimal](18, 0) NULL,
	[salestax] [decimal](18, 0) NULL,
	[profit] [decimal](18, 0) NULL,
	[quantityonhand] [decimal](18, 0) NULL,
 CONSTRAINT [PK__itemmast__986EE407B02D1FB9] PRIMARY KEY CLUSTERED 
(
	[itemcode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO




Suggesion: please do not use inline query in your application, it will allow SQL Injection[^] to be injected to your application.
Instead using inline query use store procedure with in your application like this Store Procedure Demo[^]


这篇关于将值插入SQL Server表并单击空白记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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