Solved

save file to database vb.net vs2005

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

762 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

22 Experts available now in Live!

Get 1:1 Help Now