如何在vb.net中计数并将其定向到数据库(SQLserver) [英] How to count in vb.net and direct it to the database(SQLserver)
问题描述
大家好!程序员和开发人员.
我想问一下我的项目.
在我的项目中,我有两个文本框,例如text1和text2
现在用户将输入一个整数,该整数将在计数范围内
例如,用户在text1中输入了1,在text2中输入了5
因此
它会像这样
1
2
3
4
5
也许我可以用标签显示它,但更重要的是使数字插入数据库.
总结当我输入范围时系统将进行计数并且数字将自动在数据库中.
谢谢大家.
我真的需要一些帮助.请.
更大的力量!祝你有美好的一天!期待您的帮助. ;)
Hi guys! hi programmers and dvelopers.
I would like to ask about my project.
in my project i have two textbox such as text1 and text2
now the user will input a integer that will range the count
example the user inputted 1 in text1 and 5 for text2
therefore
it will go like this
1
2
3
4
5
maybe if i can display it with a label but more important is to make the numbers insert to the database.
to summarize when i input the range the system will count and the numbers will automatically be in the database.
thanks guys.
i really need some help here. please.
more power! have a great day! looking forward for your help. ;)
推荐答案
一种有效的方法是为此创建存储过程.像
An efficient way would be to create a stored procedure for this. Something like
CREATE PROCEDURE InsertNumbers @start int, @end int
AS
BEGIN
DECLARE @counter int = @start;
WHILE (@counter <= @end) BEGIN
INSERT INTO YourTable (ColumnName) VALUES (@counter);
SET @counter = @counter + 1;
END;
END;
GO
您可以从客户端使用 ExecuteNonQuery [ ^ ]
And you can call it from client side using ExecuteNonQuery[^]
-在您的SQL上执行以下脚本
--Execute below script on your SQL
DECLARE @Start BIGINT
Declare @END BIGINT
SET @Start = 1
SET @END = 50
BEGIN
WITH mycte AS
(
SELECT @Start as id
UNION ALL
SELECT id + 1
FROM mycte
WHERE id + 1 < = @END
)
SELECT id
FROM mycte
OPTION (MAXRECURSION 0)
END
您可以使用For循环并在其中编写插入查询,这可能会对您有所帮助.
我希望您了解Ado.Net的基础知识,即如何与数据库和所有对象连接.
You can use For Loop and write insert query in that.It may help you.
I hope you know basics of Ado.Net i.e how to connect with DataBase and all.
Dim StartValue As Int32 = Val(TextBox1.Text)
Dim EndValue As Int32 = Val(TextBox2.Text)
For i As Int32 = StartValue To EndValue
Dim objCommand As OleDbCommand = New OleDbCommand 'Command Object
objCommand.Connection = Con 'Con is connection Object
objCommand.CommandText = "Insert into TableName (FieldName) Values(@FieldName)"
objCommand.SelectCommand.Parameters.AddWithValue("@FieldName", i)
Try
'Open your Connection before executiong the Query.
objCommand.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message) 'Error Message if any
End Try
objCommand = Nothing
Next
希望对您有帮助. :)
I hope it will help you. :)
这篇关于如何在vb.net中计数并将其定向到数据库(SQLserver)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!