Sunday, June 24, 2012

Full Text Search In Sql Server 2008

The first query simply returns a list of all of the catalogs in the system.
– Get current list of full text catalogs
select [name] as CatalogName
    , path
    , is_default
  from sys.fulltext_catalogs
 order by [name];

The next query returns a list of all the StopLists. 

 – Get the list of StopLists
 select stoplist_id
      , name
   from sys.fulltext_stoplists;

This query returns a list of StopWords in the database. Note the linking to get the associated StopList name and language. 

 – Get list of StopWords
 select sl.name as StopListName
      , sw.stopword as StopWord
      , lg.alias as LanguageAlias
      , lg.name  as LanguageName
      , lg.lcid  as LanguageLCID
   from sys.fulltext_stopwords sw
   join sys.fulltext_stoplists sl
    on sl.stoplist_id = sw.stoplist_id
   join master.sys.syslanguages lg
    on lg.lcid = sw.language_id;


This next query gets a list of all of the stopwords that ship with SQL Server 2008. This is a nice improvement, you can not do this in SQL Server 2005.


– Get a list of the System provided stopwords  
select ssw.stopword
    , slg.name
  from sys.fulltext_system_stopwords ssw
  join sys.fulltext_languages slg
    on slg.lcid = ssw.language_id;


My next query returns a list of all the Full Text Indexes in the database.


– List full text indexes
select c.name as CatalogName
    , t.name as TableName
    , idx.name as UniqueIndexName
    , case i.is_enabled
        when 1 then ‘Enabled’
        else ‘Not Enabled’
       end as IsEnabled
    , i.change_tracking_state_desc
    , sl.name as StopListName
  from sys.fulltext_indexes i
  join sys.fulltext_catalogs c
    on i.fulltext_catalog_id = c.fulltext_catalog_id
  join sys.tables t
    on i.object_id = t.object_id
  join sys.indexes idx
    on i.unique_index_id = idx.index_id
       and i.object_id = idx.object_id
  left join sys.fulltext_stoplists sl
    on sl.stoplist_id = i.stoplist_id




This query returns a list of all the document types SQL Server 2008 understands when they are placed in a varbinary(max) field.


– List all of the document types SQL Server 2008 will understand in varbinary(max) field
select document_type
    , path
    , [version]
    , manufacturer
  from sys.fulltext_document_types;

If your full text performance begins to suffer over time, you might want to check and see how many fragments exist. If you have multiple closed fragments, you should consider doing a REORGANIZE on the index (using alter fulltext index). This query will tell you how many fragments exist for your full text index.


– See how many fragments exist for each full text index.
– If multiple closed fragments exist for a table do a REORGANIZE to help performance
select t.name as TableName
    , f.data_size
    , f.row_count
    , case f.status
        when 0 then ‘Newly created and not yet used’
        when 1 then ‘Being used for insert’
        when 4 then ‘Closed ready for query’
        when 6 then ‘Being used for merge inpurt and ready for query’
        when 8 then ‘Marked for deletion. Will not be used for query and merge source’
        else ‘Unknown status code’
       end
  from sys.fulltext_index_fragments f
  join sys.tables t on f.table_id = t.object_id;

There you go, a handful of powerful queries to help you query and maintain the state of your full text indexes.

Upload File Form in php

This below code allow user to upload image in web page that made in php program language .. 

html code : 

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html> 


the following about the HTML form above:
  • The enctype attribute of the <form> tag specifies which content-type to use when submitting the form. "multipart/form-data" is used when a form requires binary data, like the contents of a file, to be uploaded
  • The type="file" attribute of the <input> tag specifies that the input should be processed as a file. For example, when viewed in a browser, there will be a browse-button next to the input field
Note: Allowing users to upload files is a big security risk. Only permit trusted users to perform file uploads.


Create The Upload Script

The "upload_file.php" file contains the code for uploading a file:

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?> 
 
 
By using the global PHP $_FILES array you can upload files from a client computer to the remote server.
The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error". Like this:
  • $_FILES["file"]["name"] - the name of the uploaded file
  • $_FILES["file"]["type"] - the type of the uploaded file
  • $_FILES["file"]["size"] - the size in bytes of the uploaded file
  • $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
  • $_FILES["file"]["error"] - the error code resulting from the file upload
This is a very simple way of uploading files. For security reasons, you should add restrictions on what the user is allowed to upload.

Restrictions on Upload

In this script we add some restrictions to the file upload. The user may only upload .gif or .jpeg files and the file size must be under 20 kb:
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }
?>
Note: For IE to recognize jpg files the type must be pjpeg, for FireFox it must be jpeg.

Saving the Uploaded File

The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server.
The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location:

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?> 
 
The script above checks if the file already exists, if it does not, it copies the file to the specified folder.

make rss feed in asp.net

Using the XmlTextWrite to output an XML file that serves as an RSS feed.

using System.Data.SqlClient;
using System.Text;
using System.Xml;
...
int copyrightyear = DateTime.Now.Year;
Response.Clear();
Response.ContentType = "text/xml";
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
writer.WriteStartDocument();
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteStartElement("channel");
writer.WriteElementString("title", "Mikesdotnetting News Feed");
writer.WriteElementString("link", 
  "http://www.mikesdotnetting.com/rss.aspx");
writer.WriteElementString("description", 
  "Latest additions to the rubbish that appears on Mikesdotnetting.com.");
writer.WriteElementString("copyright", "(c) " + copyrightyear.ToString() +
  ", Mikesdotnetting. All rights reserved.");
string connectionString = Utils.GetConnString();
using (SqlConnection conn = new SqlConnection(connectionString))
{
  using (SqlCommand objCommand = new SqlCommand("GetRss", conn))
  {
    objCommand.CommandType = CommandType.StoredProcedure;
    conn.Open();
    using (SqlDataReader objReader = objCommand.ExecuteReader())
    {
      while (objReader.Read())
      {
        writer.WriteStartElement("item");
        writer.WriteElementString("title", objReader.GetString(1));
        writer.WriteElementString("description", objReader.GetString(2));
        writer.WriteElementString("link", "http://www.mikesdotnetting.com/Article.aspx?ArticleID=
          " + objReader.GetInt32(0).ToString());
        writer.WriteElementString("pubDate", 
          objReader.GetDateTime(3).ToString("R"));
        writer.WriteEndElement();
      } 
      objReader.Close();
      conn.Close();
      writer.WriteEndElement();
      writer.WriteEndElement();
      writer.WriteEndDocument();
      writer.Flush();
      writer.Close();
      Response.End();
    }
  }
}

Rss File Code Project

Having an RSS feed on your website is a great way of sharing your content with the rest of the Internet. It’s not a new technology and it’s probably something that you use on a daily basis. If you have a blog or use any form of CMS, that software will most likely handle the creation of your RSS feed for you.
Sometimes, however, it might be necessary for you to create a RSS feed yourself. Perhaps you have just won a new client who’s current site has a old bespoke CMS which they like and want to keep, but you want to be able to publish their updated content via RSS. Hopefully this tutorial will help you to achieve this.

What is RSS?

RSS, in its current form, stands for Really Simple Syndication and is a family of web formats to publish frequently updated content. The RSS Feed (as it is commonly known) can then be read by the users feed reading software or by another website which wishes to ‘syndicate’ the content of that feed.

The RSS format is based on XML that is built using standardised tags. Here is an example of a basic RSS document, we will be generating something similar using PHP later in this tutorial:



01
02
03
04
05     
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
    <channel>
        <title>My RSS feed</title>
        <link>http://www.mywebsite.com/</link>
        <description>This is an example RSS feed</description>
        <language>en-us</language>
        <copyright>Copyright (C) 2009 mywebsite.com</copyright>
        <item>
            <title>My News Story 3</title>
            <description>This is example news item</description>
            <link>http://www.mywebsite.com/news3.html</link>
            <pubDate>Mon, 23 Feb 2009 09:27:16 +0000</pubDate>
        </item>
        <item>
            <title>My News Story 2</title>
            <description>This is example news item</description>
            <link>http://www.mywebsite.com/news2.html</link>
            <pubDate>Wed, 14 Jan 2009 12:00:00 +0000</pubDate>
        </item>
        <item>
            <title>My News Story 1</title>
            <description>This is example news item</description>
            <link>http://www.mywebsite.com/news1.html</link>
            <pubDate>Wed, 05 Jan 2009 15:57:20 +0000</pubDate>
        </item>
    </channel>
  </rss>




As we know, RSS is made up of standardised XML tags which you would normally find in a flat XML file. What we are going to do is create this standard XML data dynamically using PHP. This means that the URL for our feed will end with the .php extension, but we will tidy this up later in the tutorial.
So, lets start building the PHP script. The first thing we are going to do is tell PHP what type of data we would like to output (I am going to break each section of the script down, but I will include it in full at the end):

1
2
<?php
    header("Content-Type: application/rss+xml; charset=ISO-8859-1");
The header() function that we have called is telling PHP to output our data using the XML MIME type as well as to use the ISO-8859-1 character set. This makes sure that our content is delivered to the users in the correct format.
Now we are going to define our database connection details and create the header XML tags for our RSS feed. I’m going to statically assign the feed information tags just to keep it simple, however, you may wish to expand on this to dynamically set these. That way, you can re-use this code as a function or even go a step further and use it to build your own PHP class. The actual feed data will be kept in a variable called $rssfeed.
 
01
02   
03
04
05
06
07
08
09
10
11
12
13
DEFINE ('DB_USER', 'my_username');
DEFINE ('DB_PASSWORD', 'my_password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'my_database');
 
$rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$rssfeed .= '<rss version="2.0">';
$rssfeed .= '<channel>';
$rssfeed .= '<title>My RSS feed</title>';
$rssfeed .= '<link>http://www.mywebsite.com </link>';
$rssfeed .= '<description>This is an example RSS feed</description>';
$rssfeed .= '<language>en-us</language>';
$rssfeed .= '<copyright>Copyright (C) 2009 mywebsite.com</copyright>';
Next, we need to extract our data by looping through our MySQL database to create the <item> tags. I’m not going to explain this part in too much details as it’s not point of this tutorial and you may do this differently. We are going to assume that our MySQL table (“mytable”) has columns called title, description, link and date which hold the relevant data:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 <?php  
 $connection = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
        or die('Could not connect to database');
    mysql_select_db(DB_NAME)
        or die ('Could not select database');
 
    $query = "SELECT * FROM mytable ORDER BY date DESC";
    $result = mysql_query($query) or die ("Could not execute query");
 
    while($row = mysql_fetch_array($result)) {
        extract($row);
 
        $rssfeed .= '<item>';
        $rssfeed .= '<title>' . $title . '</title>';
        $rssfeed .= '<description>' . $description . '</description>';
        $rssfeed .= '<link>' . $link . '</link>';
        $rssfeed .= '<pubDate>' . date("D, d M Y H:i:s O", strtotime($date)) . '</pubDate>';
        $rssfeed .= '</item>';
    }
 
    $rssfeed .= '</channel>';
    $rssfeed .= '</rss>';
 
    echo $rssfeed;
 ?>
The first part of this section connects to the MySQL database using the constants which we defined at the start of the script. Then we perform a basic SQL query to pull out all our data from the database in date order. The final part of the query, DESC, ensures that the newest content will appear first in the users RSS reader.
Next, we look at each row of data from the results of the query using a while loop. In each loop cycle, the first action performed is the extract() function to create a set of variables that take the name of the columns of the database, in my case $title, $description, $link, and $date. We then add the data contained in these variables to our main $rssfeed variable. When it reaches the final row of data, the while loop ends and we apply the closing XML tags.
The final step of the script is to actually output the data we have collected. This is simply done by echoing the $rssfeed variable. Here is the code in full:
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
    header("Content-Type: application/rss+xml; charset=ISO-8859-1");
 
    DEFINE ('DB_USER', 'my_username');
    DEFINE ('DB_PASSWORD', 'my_password');
    DEFINE ('DB_HOST', 'localhost');
    DEFINE ('DB_NAME', 'my_database');
 
    $rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?>';
    $rssfeed .= '<rss version="2.0">';
    $rssfeed .= '<channel>';
    $rssfeed .= '<title>My RSS feed</title>';
    $rssfeed .= '<link>http://www.mywebsite.com</link>';
    $rssfeed .= '<description>This is an example RSS feed</description>';
    $rssfeed .= '<language>en-us</language>';
    $rssfeed .= '<copyright>Copyright (C) 2009 mywebsite.com</copyright>';
 
    $connection = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
        or die('Could not connect to database');
    mysql_select_db(DB_NAME)
        or die ('Could not select database');
 
    $query = "SELECT * FROM mytable ORDER BY date DESC";
    $result = mysql_query($query) or die ("Could not execute query");
 
    while($row = mysql_fetch_array($result)) {
        extract($row);
 
        $rssfeed .= '<item>';
        $rssfeed .= '<title>' . $title . '</title>';
        $rssfeed .= '<description>' . $description . '</description>';
        $rssfeed .= '<link>' . $link . '</link>';
        $rssfeed .= '<pubDate>' . date("D, d M Y H:i:s O", strtotime($date)) . '</pubDate>';
        $rssfeed .= '</item>';
    }
 
    $rssfeed .= '</channel>';
    $rssfeed .= '</rss>';
 
    echo $rssfeed;
?>


I mentioned earlier that I would tidy up the .php extension of the feed. I don’t know about you, but I find this a little bit ugly. When you see RSS feeds that have been generated by WordPress for example, you don’t see the actual file name, you just get the containing folder. To do this, create a folder in the root directory of the site and call it ‘feed’. Create a file in this new folder called ‘index.php’ and copy the code above into it. This leaves us with a nicer looking feed URL, in the case of this example, http://www.mydomain.com/feed/.

Writing a Text File

This short piece of code demonstrates how to write a text file in Java. The PrinterWriter class contains a number of methods for outputting text to a file. The FileWriter class is a convenience class for writing to a text file. It uses the default code page for the operating environment your virtual machine is running in. If you need to specify a specific code page, then you must use the OutputStreamWriter class instead.


 1:/** Simple Program to write a text file
   2:*/
   3:
   4: import java.io.*;
   5:
   6: public class WriteText{
   7:    public static void main(String[] args){
   8:        try {
   9:            FileWriter outFile = new FileWriter(args[0]);
  10:            PrintWriter out = new PrintWriter(outFile);
  11:            
  12:            // Also could be written as follows on one line
  13:            // Printwriter out = new PrintWriter(new FileWriter(args[0]));
  14:        
  15:            // Write text to file
  16:            out.println("This is line 1");
  17:            out.println("This is line 2");
  18:            out.print("This is line3 part 1, ");
  19:            out.println("this is line 3 part 2");
  20:            out.close();
  21:        } catch (IOException e){
  22:            e.printStackTrace();
  23:        }
  24:    }
  25: }