Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help changing data source from .xml to sql table

Author  Topic 

jrod11
Starting Member

1 Post

Posted - 2009-08-28 : 13:11:36
Hi everyone,
I am currently working on a project that pulls data from sql and displays it in an html file. This is working fine, but I am trying to make it a little more fancy using this cool source code found here:

[url]http://mattberseth.com/blog/2007/12/creating_a_google_suggest_styl.html[/url]

In the source code, it is pulling data from an .xml file that stores all of the data. I would like to change the source of the data to a table in my sql 2005. Can someone help me make the necessary changes to pull my data from SQL? All help is appreciated! Here is my C# code that references the .xml file;

using System;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;

/// <summary>
/// Summary description for CustomerDataObject
/// </summary>
[DataObject(true)]
public class CustomersDataObject
{
/// <summary>
///
/// </summary>
private DataSet _customers;

/// <summary>
///
/// </summary>
public CustomersDataObject()
{
this._customers = HttpContext.Current.Session["Customers"] as DataSet;

if (this._customers == null)
{
this._customers = new DataSet();
this._customers.ReadXml(HttpContext.Current.Server.MapPath(@"App_Data\customers.xml"));

HttpContext.Current.Session["Customers"] = this._customers;
}
}

/// <summary>
///
/// </summary>
public DataTable CustomerTable
{
get { return this._customers.Tables["customers"]; }
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public DataView Select(string propertyName, string propertyValue)
{
EnumerableRowCollection<DataRow> query = null;
if (string.IsNullOrEmpty(propertyName) || string.IsNullOrEmpty(propertyValue))
{
query =
from customer in this.CustomerTable.AsEnumerable()
select customer;
}
else
{
query =
from customer in this.CustomerTable.AsEnumerable()
where customer.Field<string>(propertyName).Equals(propertyValue, StringComparison.CurrentCultureIgnoreCase)
select customer;
}

return query.AsDataView();
}

/// <summary>
///
/// </summary>
/// <param name="columnName"></param>
/// <param name="count"></param>
/// <param name="prefix"></param>
/// <returns></returns>
public string[] GetCompletionList(string columnName, string prefix, int count)
{
// find all of the rows that have values that start with
// the provided prefix
EnumerableRowCollection<DataRow> query =
from customer in this.CustomerTable.AsEnumerable()
where customer.Field<string>(columnName).ToLower().StartsWith(prefix.ToLower())
select customer;

DataView view = query.AsDataView();

// only return distinct values
System.Collections.Generic.List<string> items = new System.Collections.Generic.List<string>();
#region Distinct
for (int i = 0; i < count && i < view.Count; i++)
{
string value = view[i][columnName].ToString();
if (!items.Contains(value))
{
items.Add(value);
}
}
#endregion

// return the items
return items.ToArray();
}
}
   

- Advertisement -