473,486 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

VB Array to Database?

shelzmike
11 New Member
To start with, let me lay out the situation. I am a student at the very end of my last assignment of a beginner - intermediate VB class (we have been using VB 2005 Express + MSSQL Express 2005). Anyhow, I really like coding with VB and understand most of what we have learned thus far. However, we have a final project due that is a complete build-from-scratch idea that is useful and contains 10 of 20 requirements as listed on the Rubric.

Without explaining the whole project, the final thing that I am having an issue with (mainly because we delved only once into using Databases - ADO.NET, and that was a VERY basic applicaiton and the database was already built.

Here is the basics of what I am trying to do:

This is a program that generates a sequential list of numbers, based on user input (min and max, plus total amount of numbers) to be used for a raffle type of fundraiser.

So user generates a list of numbers, they enter the lowest number and the total number of ticket/numbers. I can use a Do...While...Loop to create the list of numbers and then save those into a declared one dimensional array. All good and well, I know how to do that.

The next part is where I am getting hung up. I will have a Next or Save button that when the user clicks it, I want the array to then be saved in a database, with the generated numbers used as the Primary Key of the DB. The database will also have other fields (First Name, Last Name, Phone, Address, Email) that will be Null until a later portion of the application (where I will then enter peoples names who bought the tickets, and will then use a random number generator to pick the winner). It sounds simple enough but cannot figure out how to do it.

Also, if possible, I would love to be able to have each generation of numbers be in a different table (i.e., for more than one raffle, if the user so chooses). So I need help on creating a database (the ones we have used so far have already been created, we just manipulated them) and saving the array as listed above - the other fields will be aded from another form, and I know how to do that.

I am looking for the simplest way to achieve this as it is a simple application.

Thanks!

Mike
Oct 10 '08 #1
4 2534
debasisdas
8,127 Recognized Expert Expert
try to fetch tha value from the arrray and insert to the database till the end of the array.
Oct 10 '08 #2
rpicilli
77 New Member
Hi there.

There is no easy way to create a database and table from scratch. In fact this will take from you a lot of efforts and line of code.

The best way to do that is create the database and table by hand and after that ask you database software (Access or Sql) to generate the SQL Script.

As exemple here you find a sql script to create a Database named Test and a Table named myTable.


Expand|Select|Wrap|Line Numbers
  1.  
  2. IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'Test')
  3.     DROP DATABASE [Test]
  4. GO
  5.  
  6. CREATE DATABASE [Test]  ON (NAME = N'Test_Data', FILENAME = N'D:\Arquivos de programas\Microsoft SQL Server\MSSQL\data\Test_Data.MDF' , SIZE = 1, FILEGROWTH = 10%) LOG ON (NAME = N'Test_Log', FILENAME = N'D:\Arquivos de programas\Microsoft SQL Server\MSSQL\data\Test_Log.LDF' , SIZE = 1, FILEGROWTH = 10%)
  7.  COLLATE Latin1_General_CI_AS
  8. GO
  9.  
  10. use [Test]
  11. GO
  12.  
  13. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MyTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  14. drop table [dbo].[MyTable]
  15. GO
  16.  
  17. CREATE TABLE [dbo].[MyTable] (
  18.     [id] [int] NULL ,
  19.     [FirstName] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  20.     [LastName] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  21.     [BornDt] [smalldatetime] NULL ,
  22.     [Gender] [char] (1) COLLATE Latin1_General_CI_AS NULL 
  23. ) ON [PRIMARY]
  24. GO
  25.  
  26.  
As you can see, in your project you'll a lot to type.

By the description of project you are working, I think you will have more issues in near future. As you mencioned you'll save in a data base a certain number of "number" and in the future complete this table with other data. If those numbers has any connection with the names, how you'll know which number is attached to which name?

Good luck

Rpicilli
Oct 10 '08 #3
shelzmike
11 New Member
Thanks for the info. I came across this code on MSDN that seems to be doing what I need it to. All I want to do intially is set up a blank database that has two tables - one that has a column for the Raffle Numbers (generated and saved from Array) and the date of the generation. The Raffle number will be the PK, then on the second table, I will have fields that contain the following: Ticket ID (which is the PK also and is the same raffle numbers as the other table), First Name, Last Name, Contact Info. Then I will use another form that is is binded to the second database so that I can use the Navigator (or search) to sort through the numbers and enter peoples names as they are sold.

So essentially, why couldn't I just use the code (below) to create the database? And if I did, what code do I add to manipulate it so I can create the different tables, columns, and keys. By the way, I do have Access and could use that - I have a lot more knowledge with Access, would that be easier at all? I really wanted to use SQL though since I want to learn better how to use it. Thanks!

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnCreateDatabase_Click(ByVal sender As System.Object, _
  2. ByVal e As System.EventArgs) Handles btnCreateDatabase.Click
  3.     Dim str As String
  4.  
  5.     Dim myConn As SqlConnection = New SqlConnection("Server=(local)\netsdk;" & _
  6.                                                     "uid=sa;pwd=;database=master")
  7.  
  8.     str = "CREATE DATABASE MyDatabase ON PRIMARY " & _
  9.           "(NAME = MyDatabase_Data, " & _
  10.           " FILENAME = 'D:\MyFolder\MyDatabaseData.mdf', " & _
  11.           " SIZE = 2MB, " & _
  12.           " MAXSIZE = 10MB, " & _
  13.           " FILEGROWTH = 10%) " & _
  14.           " LOG ON " & _
  15.           "(NAME = MyDatabase_Log, " & _
  16.           " FILENAME = 'D:\MyFolder\MyDatabaseLog.ldf', " & _
  17.           " SIZE = 1MB, " & _
  18.           " MAXSIZE = 5MB, " & _
  19.           " FILEGROWTH = 10%) "
  20.  
  21.     Dim myCommand As SqlCommand = New SqlCommand(str, myConn)
  22.  
  23.     Try
  24.         myConn.Open()
  25.         myCommand.ExecuteNonQuery()
  26.         MessageBox.Show("Database is created successfully", _
  27.                         "MyProgram", MessageBoxButtons.OK, _
  28.                          MessageBoxIcon.Information)
  29.        Catch ex As Exception
  30.            MessageBox.Show(ex.ToString())
  31.        Finally
  32.            If (myConn.State = ConnectionState.Open) Then
  33.                myConn.Close()
  34.            End If
  35.        End Try
  36.  
  37. End Sub
  38.  
Oct 10 '08 #4
debasisdas
8,127 Recognized Expert Expert
So essentially, why couldn't I just use the code (below) to create the database?
Simply because database is not created at run time, even though you can do that.
Oct 10 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

12
4291
by: James | last post by:
Hi, Have posted before, but will simplify problem here. For original post go to http://forums.devshed.com/t80025/s.html I have setup 2 arrays like so in my one page script: $carers =...
5
4476
by: Joel Farris | last post by:
I've just finished reading Kevin Yank's book "Build Your Own Database Driven Website", and I've not quite got a handle on the array variable. Why do I need an array? Give me an example of an...
4
2170
by: John Hoge | last post by:
I'm looking to make an admin page for a web photo gallery, and I need to make a drop down menu showing all files in a given directory that do not have comments in a database for the given filename....
23
3228
by: Rob Meade | last post by:
Lo all, Ok - this is what I was aiming to do, and then I thought - naahhh, that cant be right! query database results to recordset results to array using GetRows update values in one column...
0
1380
by: EMiller | last post by:
Hello, I am encountering a development challenge here that seems to be stumping me. I am developing a C#/.NET application using an MSDE database. There is a particular field in a table that I...
1
1751
by: garimapuri | last post by:
hi ihad an array in php and iwant to insert its value in database the coding is: <?php $nv_pairs = explode('&',$query_string); $array; list ($name, $value) = explode ('=',$nv_pairs); $i = 0;...
4
5014
by: Haydnw | last post by:
Hi, I'd like to put a load of database results (several rows for 5 fields) into a two-dimensional array. Now, this may be a really stupid question, but can someone give me a pointer for how to...
7
11763
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
2
15485
by: assgar | last post by:
I am having problems getting the user selected form info to inserted into the mysql database. I am also rec eving an error: Warning: Variable passed to each() is not an array or object in ...
5
6620
by: jmDesktop | last post by:
In my code I cannot figure out how to retrieve multple rows from my returned array from a class method. I have tried: <?php class myClass { private $connection; /* Class constructor */...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.