On 5/30/10, Puspa <[email protected]> wrote:
>
> Hi All,
> I need to check data inside the table of sql server.If the data
> exist,it show the existence of data.Actually i used for loop for
> checking,
> but it didn't give proper result.so i need help from u that how to
> check exist data in sql server using dataset?I need coding in c#.
>
> Thanks in advance
I have given 2 example check
You can use foreach loop
* Looping over DataTable instance (C#) ---*
using System;
using System.Data;
class Program
{
static void Main()
{
DataTable table = *GetTable()*; *// Get the data table.*
foreach (DataRow row in table.*Rows*) *// Loop over the rows.*
{
Console.WriteLine("--- Row ---"); *// Print separator.*
foreach (var item in row.*ItemArray*) *// Loop over the items.*
{
Console.Write("Item: "); *// Print label.*
Console.WriteLine(item); *// Invokes ToString abstract method.*
}
}
Console.Read(); *// Pause.*
}
*/// <summary>
/// Generates DataTable filled with patient information.
/// </summary>*
static DataTable *GetTable()*
{
DataTable table = new *DataTable*(); *// New data table.*
table.Columns.Add("Dosage", typeof(int)); *// Add five columns.*
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
table.Rows.Add(15, "Abilify", "Jacob", DateTime.Now); *// Add
five data rows.*
table.Rows.Add(40, "Accupril", "Emma", DateTime.Now);
table.Rows.Add(40, "Accutane", "Michael", DateTime.Now);
table.Rows.Add(20, "Aciphex", "Ethan", DateTime.Now);
table.Rows.Add(45, "Actos", "Emily", DateTime.Now);
return table; *// Return reference.*
}
}
this is console program
*class *MainClass
{
*static **void *Main(string[] args)
{
string connString = @
"server = .\sqlexpress;integrated security = true;database = northwind";
string sql = @
"select productname,unitprice from products where unitprice < 20";
SqlConnection conn = *new *SqlConnection(connString);
*try*
{
conn.Open();
SqlDataAdapter da = *new *SqlDataAdapter(sql, conn);
DataTable dt = *new *DataTable();
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
Console.WriteLine(row[col]);
}
}
*catch*(Exception e)
{
Console.WriteLine("Error: " + e);
}
*finally*
{
conn.Close();
}
}
}