Solved

save file to database vb.net vs2005

Posted on 2007-04-09
3
280 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

815 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

8 Experts available now in Live!

Get 1:1 Help Now