如何在数据库中添加值 [英] how to add values in database

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

问题描述

我有一个数据库,其中的值将在某个时间间隔内输入。

喜欢

value1,value2,value3 ....... value11 with date( date.time.now)

现在我的问题是如何将数据添加到数据库(我已经执行了插入命令)

但这是插入数据附加行。

i希望同一天的所有数据都在一行中,

我的代码是:

i have a database where the values will be entered on some interval.
like
value1,value2,value3.......value11 with date (date.time.now)
now my question is this that how can i add the data to database (i have performed the insert command)
but this is inserting the data with additional Row.
i want that all the data of the same date should be in one row,
my code is :

SqlConnection con = new SqlConnection("//hideen the database//");
SqlDataAdapter myadp = new SqlDataAdapter();
myadp.InsertCommand = new SqlCommand();
myadp.InsertCommand.Connection = con;
con.Open();
myadp.InsertCommand.CommandText = "insert into number values(@Morning,@Super,@Delux,@Afternoon,@Dimand,@Moti,@Gold,@Silver,@Evening,@Express,@Night,@Datee)";
myadp.InsertCommand.Parameters.AddWithValue("@Morning",TextBox3.Text);
myadp.InsertCommand.Parameters.AddWithValue("@Super", TextBox4.Text);
myadp.InsertCommand.Parameters.AddWithValue("@Delux", TextBox5.Text);
myadp.InsertCommand.Parameters.AddWithValue("@Afternoon", TextBox6.Text);
myadp.InsertCommand.Parameters.AddWithValue("@Dimand", TextBox7.Text);
myadp.InsertCommand.Parameters.AddWithValue("@Moti", TextBox8.Text);
myadp.InsertCommand.Parameters.AddWithValue("@Gold", TextBox9.Text);
myadp.InsertCommand.Parameters.AddWithValue("@Silver", TextBox10.Text);
myadp.InsertCommand.Parameters.AddWithValue("@Evening", TextBox11.Text);
myadp.InsertCommand.Parameters.AddWithValue("@Express", TextBox12.Text);
myadp.InsertCommand.Parameters.AddWithValue("@Night", TextBox13.Text);
myadp.InsertCommand.Parameters.AddWithValue("@Datee", Label1.Text);
int i = myadp.InsertCommand.ExecuteNonQuery();
if (i == 1)
{
    Response.Write("<script>alert('number updated sucessfully')</script>");
}
else
{
    Response.Write("<script>alert('somthing went wrong try again')</script>");
}
con.Close();

推荐答案

这是正确的,'insert'查询将始终创建表格中的新行。如果你想保留一个特定的行,那就是'更新',但你必须在'WHERE'子句中指定要更新的行,如下所示:

That is correct, 'insert' query will always create a new row in the table. If you want to keep to a particular row, that is 'update', but you have to specify which row to update in the 'WHERE' clause like this:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;



了解详情: sql_update [ ^ ]


你可以使用 SQL:如果存在更新其他插入 [ ^ ]。



如果日期上存在任何记录,则更新,否则插入记录。
You can use SQL: If Exists Update Else Insert[^].

If any record exists on the Date, then Update it, otherwise Insert the record.


如果你有所有字段信息,你可以尝试使用这个sql语句

You can try using this sql sentence if you have all fields information
INSERT OR REPLACE INTO [table] (field1, field2,...) VALUES (value1, value2,...);



自动sql server选择正确的句子


to automatically sql server choose the correct sentence


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

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