Solved

save file to database vb.net vs2005

Posted on 2007-04-09
3
277 Views
Last Modified: 2013-12-17
I have the prototype of a screen where someone can browse for a file.  It is supposed to be imported into the table below.  I plan to use a dataset built in the designer.  How do I save the file to the db?

CREATE TABLE [Banks].[tblBankAttachments](
      [BankAttachmentGUID] [PCA].[UDT_GUID] NOT NULL,
      [BankGUID] [PCA].[UDT_GUID] NOT NULL,
      [Filename] [varbinary](50) NOT NULL,
      [Filedescription] [nchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [FileSize] [int] NOT NULL,
      [FileText] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [CreatedOn] [datetime] NOT NULL,
      [CreatedBy] [nvarchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 CONSTRAINT [PK_tblBankAttachments] PRIMARY KEY CLUSTERED
0
Comment
Question by:jackjohnson44
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
VBRocks earned 500 total points
ID: 18877670
What type of database are you trying to write to?

Here is an example of how you can do it with SQL:

Assuming that you have a strongly typed dataset created with 1 table named "Banks"
Dim cn As New SqlClient.SqlConnection("Your Connection String")

Dim da As New SqlClient.SqlDataAdapter()
da.InsertCommand = New SqlClient.SqlCommand("CommandTextOrStoredProcedure", cn)

'Use whichever CommandType is appropriate for you
da.InsertCommand.CommandType = CommandType.StoredProcedure
'da.InsertCommand.CommandType = CommandType.Text

With da.InsertCommand.Parameters
     .Add(New SqlClient.SqlParameter("@BankAttachmentGUID", SqlDbType.Udt, 0, "BankAttachmentGUID"))
     .Add(New SqlClient.SqlParameter("@BankGUID", SqlDbType.Udt, 0, "BankGUID"))
     .Add(New SqlClient.SqlParameter("@Filename", SqlDbType.VarBinary, 50, "Filename"))
     .Add(New SqlClient.SqlParameter("@Filedescription", SqlDbType.NChar, 255, "Filedescription"))
     .Add(New SqlClient.SqlParameter("@FileSize", SqlDbType.Int, 0, "FileSize"))
     .Add(New SqlClient.SqlParameter("@FileText", SqlDbType.VarChar, 8000, "FileText"))
     .Add(New SqlClient.SqlParameter("@CreatedOn", SqlDbType.DateTime, 0, "FileText"))
     .Add(New SqlClient.SqlParameter("@CreatedBy", SqlDbType.NVarChar, 30, "CreatedBy"))

End With

'Update Command
da.UpdateCommand = New SqlClient.SqlCommand("InsertCommandTextOrStoredProcedure", cn)

'Whichever is appropriate
da.UpdateCommand.CommandType = CommandType.StoredProcedure
'da.UpdateCommand.CommandType = CommandType.Text

With da.UpdateCommand.Parameters
     .Add(New SqlClient.SqlParameter("@BankAttachmentGUID", SqlDbType.Udt, 0, "BankAttachmentGUID"))
     .Add(New SqlClient.SqlParameter("@BankGUID", SqlDbType.Udt, 0, "BankGUID"))
     .Add(New SqlClient.SqlParameter("@Filename", SqlDbType.VarBinary, 50, "Filename"))
     .Add(New SqlClient.SqlParameter("@Filedescription", SqlDbType.NChar, 255, "Filedescription"))
     .Add(New SqlClient.SqlParameter("@FileSize", SqlDbType.Int, 0, "FileSize"))
     .Add(New SqlClient.SqlParameter("@FileText", SqlDbType.VarChar, 8000, "FileText"))
     .Add(New SqlClient.SqlParameter("@CreatedOn", SqlDbType.DateTime, 0, "FileText"))
     .Add(New SqlClient.SqlParameter("@CreatedBy", SqlDbType.NVarChar, 30, "CreatedBy"))

End With

da.Update(YourDataSet.Banks)     'Banks is the name of the table in the dataset

Hope that is helpful.
0
 

Author Comment

by:jackjohnson44
ID: 18878421
thanks,

sql, but how do I get a file into the variable or check the size?

If I start with a file browse dialogue, where do I go from there?
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18884984
What type of a data file are they opening up?  A CSV (comma separated value) file, XML, a TXT file, etc?

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now