Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 9th, 2007, 02:54 PM
Authorized User
 
Join Date: Feb 2007
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to toddw607 Send a message via Yahoo to toddw607
Default xml href from sql server

 I am trying to place an entire XML file into SQL Server (not a table an actual file). I have a aspx page that inserts data into the SQL Server and when I type in the href it brings it in but I cannot go anywhere from it. Is there any way to place an entire xml file into SQL Server or at least a href. I'm aware how this could be dangerous, I'm just trying to get it started right now though, any ideas?

 
Old May 9th, 2007, 04:47 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My suggestion is save the file not in SQL but out on a file share somewhere and store a path and file name (link) to the file in the database.

 
Old May 9th, 2007, 05:01 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You may want to take a look here to get some ideas on how to do this with ASP.NET:

http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=414

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old May 10th, 2007, 08:03 AM
Authorized User
 
Join Date: Feb 2007
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to toddw607 Send a message via Yahoo to toddw607
Default

Your article is awesome!! I'm about half way through it and it has taught me so much. I'm not used to using C# so I picked that one just to get familiar with the code. Thanks for helping me out! I may have a few small questions when I'm finished but I just wanted to say thanks. :-)

 
Old May 10th, 2007, 10:29 AM
Authorized User
 
Join Date: Feb 2007
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to toddw607 Send a message via Yahoo to toddw607
Default

I just had a few questions. I created a table in SQL Server to hold five values: id, fileUrl, filedata, originalName and contentType. I also created a stored procedure that selects all these fields and called it sprocFileInfoSelectList, as in the code. However, when I try to bring it up in the browser it gives me the error:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: DateCreated

   It says that the error occurs on Line 77 of the file FileInfo.cs. I think this is due to the stored procedure I created. When I comment out that line of code, it still give me the "add new file" button and when I do the file is correctly uploaded to the table in the database except that the FileUrl column has a <NULL> element in every field. I would like it to be viewed in the browser as the original one did with the "view" hyperlink. What could I be doing wrong and is there any way around this? Thanks again!

 
Old May 10th, 2007, 12:07 PM
Authorized User
 
Join Date: Feb 2007
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to toddw607 Send a message via Yahoo to toddw607
Default

I figured it out. At the end of the FileInfo file I needed to replace "return null" with "replace myList". The Gridview comes up fine now. I just have one more question: When I click on view to see the file, it gives me an error saying that:


    Procedure 'sprocFilesInsertSingleItem' expects parameter '@fileUrl', which was not supplied.

     Am I unable to view files in the database? How can I fix this?

 
Old May 10th, 2007, 05:05 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You should check the reason for the error message. Apparently, you're not passing a parameter called @fileUrl to the stored procedure.

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old May 11th, 2007, 01:18 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Read the article section with this header carefully, its well stated:

"Database - Disadvantages"

If its a lot of data, don't store it in the database. If it's a small app with little likelyhood to grow it is ok to put in the database.

 
Old May 14th, 2007, 06:00 AM
Authorized User
 
Join Date: Feb 2007
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to toddw607 Send a message via Yahoo to toddw607
Default

Hi,
    I posted to the site before I realized that you already posted here, sorry about that. I tried taking out the @fileUrl and it then states that it is not receiving my next variable. My problem is that I'm not passing nay of the arguments. Here is the code that calls the procedure:


                  public static File GetItem(Guid fileId)
  {
    File myFile = null;
    using (SqlConnection mySqlConnection = new SqlConnection(AppConfiguration.ConnectionString))
    {
        SqlCommand myCommand = new SqlCommand("sprocFilesInsertSingleItem", mySqlConnection);
      myCommand.CommandType = CommandType.StoredProcedure;

      SqlParameter prmId = new SqlParameter("@id", SqlDbType.UniqueIdentifier);
      prmId.Value = fileId;
      myCommand.Parameters.Add(prmId);

      mySqlConnection.Open();
      using (SqlDataReader myReader = myCommand.ExecuteReader())
      {
        if (myReader.Read())
        {
          myFile = new File(myReader);
        }
        myReader.Close();
      }
      mySqlConnection.Close();
    }
    return myFile;
  }

  /// <summary>
  /// Saves a file to the database.
  /// </summary>
  /// <returns>Returns true when the file was stored succesfully, or false otherwise.</returns>
  public bool Save()
  {
    return Save(DataStoreType.Database, String.Empty);
  }

  /// <summary>
  /// Saves a file to the file system.
  /// This method also saves the meta data of the file to the database.
  /// </summary>
  /// <param name="filePath">The location and name of the file that is to be saved.</param>
  /// <returns>
  /// Returns true when the file was stored succesfully, or false otherwise.
  /// </returns>
  public bool Save(string filePath)
  {
    return Save(DataStoreType.FileSystem, filePath);
  }

  /// <summary>
  /// Saves a file to the database and optionally to disk.
  /// </summary>
  /// <returns>Returns true when the file was stored succesfully, or false otherwise.</returns>
  private bool Save(DataStoreType dataStoreType, string filePath)
  {
    using (SqlConnection mySqlConnection = new SqlConnection(AppConfiguration.ConnectionString))
    {
      // Set up the Command object
      SqlCommand myCommand = new SqlCommand("sprocFilesInsertSingleItem", mySqlConnection);
      myCommand.CommandType = CommandType.StoredProcedure;

      // Set up the ID parameter
      SqlParameter prmId = new SqlParameter("@id", SqlDbType.UniqueIdentifier);

      prmId.Value = id;
      myCommand.Parameters.Add(prmId);

      // Set up the FileUrl parameter
      SqlParameter prmFileUrl = new SqlParameter("@fileUrl", SqlDbType.NVarChar, 255);

      // If we need to store the file on disk, save the fileUrl.
      if (dataStoreType == DataStoreType.FileSystem)
      {
        prmFileUrl.Value = fileUrl;
      }
      else
      {
        prmFileUrl.Value = DBNull.Value;
      }
      myCommand.Parameters.Add(prmFileUrl);

      // Set up the FileData parameter
      SqlParameter prmFileData = new SqlParameter("@fileData ", SqlDbType.Image);

      // If we need to store the file in the database,
      // pass in the actual file bytes.
      if (dataStoreType == DataStoreType.Database)
      {
        prmFileData.Value = fileData;
        prmFileData.Size = fileData.Length;
      }
      else
      {
        prmFileData.Value = DBNull.Value;
      }
      myCommand.Parameters.Add(prmFileData);

      // Set up the OriginalName parameter
      SqlParameter prmOriginalName = new SqlParameter("@originalName", SqlDbType.NVarChar, 50);
      prmOriginalName.Value = originalName;
      myCommand.Parameters.Add(prmOriginalName);

      // Set up the ContentType parameter
      SqlParameter prmContentType = new SqlParameter("@contentType", SqlDbType.NVarChar, 50);
      prmContentType.Value = contentType;
      myCommand.Parameters.Add(prmContentType);

      // Execute the command, and clean up.
      mySqlConnection.Open();
      bool result = myCommand.ExecuteNonQuery() > 0;
      mySqlConnection.Close();

      // Database update is done; now store the file on disk if we need to.
      if (dataStoreType == DataStoreType.FileSystem)
      {
        const int myBufferSize = 1024;
        Stream myInputStream = new MemoryStream(fileData);
        Stream myOutputStream = System.IO.File.OpenWrite(filePath);

        byte[] buffer = new Byte[myBufferSize];
        int numbytes;
        while ((numbytes = myInputStream.Read(buffer, 0, myBufferSize)) > 0)
        {
          myOutputStream.Write(buffer, 0, numbytes);
        }
        myInputStream.Close();
        myOutputStream.Close();
      }

      return result;
    }
  }
  #endregion

  #region Constructor(s)

  /// <summary>
  /// Initializes a new instance of the <see cref="File"/> class with the data from the SqlDataReader.
  /// </summary>
  /// <param name="myReader">A SqlDataReader that contains the data for this file.</param>
  public File(SqlDataReader myReader)
  {
    id = myReader.GetGuid(myReader.GetOrdinal("Id"));
   // dateCreated = myReader.GetDateTime(myReader.GetOrdinal("DateCrea ted"));
    originalName = myReader.GetString(myReader.GetOrdinal("OriginalNa me"));
    contentType = myReader.GetString(myReader.GetOrdinal("ContentTyp e"));

    if (!myReader.IsDBNull(myReader.GetOrdinal("FileData" )))
    {
      fileData = (byte[])myReader[myReader.GetOrdinal("FileData")];
      containsFile = true;
    }
    else
    {
      fileUrl = myReader.GetString(myReader.GetOrdinal("FileUrl")) ;
      containsFile = false;
    }
  }

  /// <summary>
  /// Initializes a new instance of the File class with the data from the incoming parameters.
  /// </summary>
  /// <param name="contentType">The content type of the file, like image/pjpeg or image/gif.</param>
  /// <param name="originalName">The original name of the uploaded file.</param>
  /// <param name="fileData">A byte array with the actual file data.</param>
  public File(string contentType, string originalName, byte[] fileData)
  {
    this.id = Guid.NewGuid();
    this.contentType = contentType;
    this.fileData = fileData;
    this.originalName = originalName;

    string extension = Path.GetExtension(originalName);
    string fileName = this.Id.ToString() + extension;
    this.fileUrl = fileName;
  }

  #endregion

}


    I think it is just like the code that you had with only a few changes. Can you see something small I just forgotten? Thanks for helping me.

 
Old May 14th, 2007, 06:24 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

There has to be a 1 on 1 mapping between stored procedure parameters and the values you pass it (unless you're using optional parameters). So, with this code:

 SqlParameter prmId = new SqlParameter("@id", SqlDbType.UniqueIdentifier);


the procedure can only have a single parameter called @id.

The same applies to the other procedures.

So, if the code is barking that it is expecting some parameters, either pass them to the procedure, or remove them from it so it's no longer expecting them....

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Server 2005 XML: FOR XML PATH -> cdata? stoves SQL Server 2005 1 July 8th, 2008 02:40 AM
Saving XML thru href link using XSLT kaukabhishek XSLT 16 June 25th, 2008 07:16 PM
sql server xml mjmcs13 XML 1 February 12th, 2007 12:50 PM
XML value in HRef link aware XSLT 3 January 8th, 2007 08:52 AM
XML from SQL Server data spinout XML 5 September 25th, 2004 05:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.