Sweety

Sweety

  • NA
  • 98
  • 110.8k

conversion of sql table to excel table

Jun 20 2011 3:58 AM

In my application i want database data expotred to excel sheet and i have to make this file as readonly and i want to display message if the user try to edit the excel Like-you are not allowed to edit this sheet im doing this in console application

I have written code like this:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data.SqlClient; 
using System.Data.Odbc; 
 
namespace ConsoleApplication1{ 
 
class Program 
{ 
 
   
static void Main(string[] args) 
   
{ 
 
       
SqlConnection con = new SqlConnection 
         
(""); 
        con
.Open(); 
       
SqlCommand cmd = new SqlCommand 
       
("select Temprature,time,date from temprature3 ", con); 
       
SqlDataReader data = cmd.ExecuteReader(); 
       
OdbcConnection dcon = new OdbcConnection(); 
        dcon
.ConnectionString = @"Driver={Microsoft Excel Driver(*.xls)};Create_DB=C:\Documents andSettings\Administrator\Desktop\Excel\Report.xls;DBQ=C:\Documents and Settings\Administrator\Desktop\Excel\Report.xls; 
  ReadOnly=True"
; 
        dcon
.Open(); 
       
OdbcCommand dcom=new OdbcCommand 
           
("create table Report new 
           (Temperature double,Time datetime,Date datetime)"
,dcon); 
        dcom
.ExecuteNonQuery(); 
       
Console.WriteLine("table has been created"); 
       
OdbcCommand dinsert=new OdbcCommand("insert into Report values(?,?,?)",dcon); 
       
OdbcParameter Temprature=new OdbcParameter("@Temperature",OdbcType.Double); 
       
OdbcParameter Time=new OdbcParameter("@Temperature",OdbcType.DateTime); 
       
OdbcParameter Date=new OdbcParameter("@Temperature",OdbcType.DateTime); 
        dinsert
.Parameters.Add(Temprature); 
        dinsert
.Parameters.Add(Time); 
        dinsert
.Parameters.Add(Date); 
       
int count=0; 
       
while(data.Read()) 
       
{ 
           
Temprature.Value=data[0]; 
           
Time.Value=data[1]; 
           
Date.Value=data[2]; 
            dinsert
.ExecuteNonQuery(); 
            count
=count+1; 
 
       
} 
 
       
Console.WriteLine("------------------------------------------------------"); 
       
Console.WriteLine(count+"Number of rows are exported to excel"); 
       
Console.WriteLine("-------------------------------------------------------"); 
       
Console.ReadLine(); 
   
} 
} 
} 

i'm gettinh error like this:

ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified


Answers (1)