sqlparameter方法数据添加 [英] sqlparameter method data adding

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

问题描述

大家好,

我在数据库中使用了三个数据,例如产品,价格和折扣.
我将折扣的默认值设为0.

sql是

Hi All,

I Used Three Data In DataBase like product, price and discount.
I give default value of discount as 0.

sql is

create table products(product nvarchar(max),
                  price numeric(18,2),
                  discount numeric(18,2) default 0)


后面的代码是


code behind is

com = New SqlCommand(";INSERT INTO products (product,price ,discount ) VALUES(@C1, @C2, @C3)";, con)
      com.Parameters.AddWithValue("@C1", TextBox1.Text)
      com.Parameters.AddWithValue("@C2", TextBox2.Text)
      com.Parameters.AddWithValue("@C3", TextBox3.Text)
      com.ExecuteNonQuery()
      MsgBox("added")



并尝试这个



and also try this

com.CommandText = "INSERT INTO Products (Product, Price, Discount) VALUES (@p1, @p2, @p3)"
        com.Connection = con

        Dim parameter As SqlParameter
        parameter = New SqlParameter("p1", System.Data.SqlDbType.NVarChar, 2000)
        parameter.Value = TextBox1.Text
        com.Parameters.Add(parameter)


        parameter = New SqlParameter("p2", System.Data.SqlDbType.Decimal)
        parameter.Value = TextBox2.Text
        com.Parameters.Add(parameter)



        parameter = New SqlParameter("p3", System.Data.SqlDbType.Decimal)
        parameter.Value = TextBox3.Text
        com.Parameters.Add(parameter)
        Try
            com.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


我想念Default,这两个方法也显示错误,请输入字段.
当我运行程序时,它显示错误,例如(``''附近的语法不正确)''(或)无法将字符串转换为int.

为此请帮我


在这两个程序中,我们输入所有输入,然后得出结果.如果未输入任何内容,则表明数据库中存在错误,允许所有数据为null,并且还提供默认值.


I miss Default those two methods also show an error, field to enter input.
When I Run The Program, it shows error like (Incorrect Syntax Near '')'' (Or) can''t convert string to int.

Please Help Me For This


In those two program we enter all input it give result. If didn''t enter any input it shows error in database allow null to all data, and give default value also.

推荐答案

该错误消息不会使感觉.您确定来自这些陈述吗?

在第一次尝试中,尝试修改:
The error message doesn''t make sense. Are you sure it''s coming from these statements?

In the first try, try modifying:
com = New SqlCommand("INSERT INTO products (product,price ,discount ) VALUES(@C1, @C2, @C3)", con)



第二次尝试,那是我的错误.在参数名称前使用@.喜欢:



And the second try, that was my mistake. use @ before the parameter names. Like:

parameter = New SqlParameter("@p1", System.Data.SqlDbType.NVarChar, 2000)


You try this:-
<pre>com = New SqlCommand("INSERT INTO products (product,price ,discount ) VALUES(@C1, @C2, @C3)", con)
      com.Parameters.Add(new SqlParameter("@C1", TextBox1.Text));
      com.Parameters.Add(new SqlParameter("@C2", TextBox2.Text));
      com.Parameters.Add(new SqlParameter("@C3", TextBox3.Text));
con.open();
      com.ExecuteNonQuery()
      MsgBox("added")
</pre>
IN your 2nd method add "@" synpole all sql parmameter for Example
In your second method you write
<pre>
 parameter = New SqlParameter("p1", System.Data.SqlDbType.NVarChar, 2000)
</pre>
but you try this
<pre>
 parameter = New SqlParameter("@p1", System.Data.SqlDbType.NVarChar, 2000)
</pre>
I hope work successfully


请考虑更改
Please consider changing
create table products(product nvarchar(max),
                  price numeric(18,2),
                  discount numeric(18,2) default 0)


变成类似
的东西


into something like

create table product_categories
(
 id bigint IDENTITY not null primary key,
 name nvarchar(255) not null, 
 CONSTRAINT UNQ_PRODUCT_CATEGORY_NAME UNIQUE(NAME)
)
go
create table products
(
 id bigint IDENTITY not null primary key,
 name nvarchar(255) not null, 
 Category bigint not null,
 description nvarchar(max),
 CONSTRAINT FK_PRODUCT_CATEGORY FOREIGN KEY(Category) REFERENCES product_categories(id),
 CONSTRAINT UNQ_PRODUCT_NAME UNIQUE(Category,name)
)
go
create table product_price
(
 id bigint IDENTITY not null primary key,
 product bigint not null,
 fromTime dateTime2 not null default SYSDATETIME(),
 price numeric(18,2),
 discount numeric(18,2) default 0,
 CONSTRAINT FK_product_price_PRODUCT FOREIGN KEY(product) REFERENCES products(id),
 CONSTRAINT UNQ_PRODUCT_PRICE UNIQUE(product,fromTime)
)
go


您所拥有的不是很有用,请考虑进一步分解,因为折扣在一定时间内并不真正与产品的基本价格一起出现...

问候
Espen Harlinn


What you''ve got isn''t very useful, and consider decomposing it even further, as discount doesn''t really belong together with the base price for the product at a given time ...

Regards
Espen Harlinn


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

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