|
I can't remember anymore where I found the original for this, but i've enhanced it a bit though.
Read the warning comments about double vs. decimal. (If someone has a solution I'd be happy to know about it!)
Private Function OS(ByVal Word As String) As String<br />
Dim i As Integer = Word.IndexOf(".")<br />
While i > -1<br />
Word = Word.Remove(i, 1)<br />
i = Word.IndexOf(".")<br />
End While<br />
Return Word<br />
End Function<br />
<br />
Public Sub ExportDatasetToExcel(ByVal Location As String, ByVal DS As DataSet)<br />
<br />
If My.Computer.FileSystem.FileExists(Location) Then<br />
Try<br />
My.Computer.FileSystem.DeleteFile(Location)<br />
Catch ex As System.IO.IOException<br />
Dim MsgStr As String = "The file is in use by another process. Close all programs that might use the file and try again."<br />
MsgBox(MsgStr, MsgBoxStyle.Exclamation)<br />
Exit Sub<br />
Catch ex As Exception<br />
Dim MsgStr As String = String.Format("This shouldn't happen, call support{0}Error message:{0}{1}{0}{0}Stacktrace:{0}{2}", Environment.NewLine, ex.Message, ex.StackTrace)<br />
MsgBox(MsgStr, MsgBoxStyle.Exclamation)<br />
Exit Sub<br />
End Try<br />
End If<br />
Dim CreateString As New StringBuilder<br />
Dim Columns As New StringBuilder<br />
Dim Mark As New StringBuilder<br />
Dim ConnectionStr As String<br />
ConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Location & ";Extended Properties=""Excel 8.0;HDR=YES"""<br />
Using Connection As New OleDb.OleDbConnection(ConnectionStr)<br />
Connection.Open()<br />
For Each table As DataTable In DS.Tables<br />
CreateString.AppendFormat("CREATE TABLE [{0}] (", table.TableName)<br />
Columns.Append("(")<br />
Mark.Append("(")<br />
For Each Column As DataColumn In table.Columns<br />
CreateString.Append(OS(Column.ColumnName))<br />
Select Case Column.DataType.Name<br />
Case "SByte", "Byte", "Int16", "Int32", "Int64", "Decimal", "Double", "Single"<br />
CreateString.Append(" Number, ")<br />
Case "Boolean"<br />
CreateString.Append(" Bit, ")<br />
Case "Char", "String"<br />
CreateString.Append(" Memo, ")<br />
Case "DateTime"<br />
CreateString.Append(" DateTime, ")<br />
Case Else<br />
CreateString.Append(" Text, ")<br />
End Select<br />
Columns.AppendFormat("{0}, ", OS(Column.ColumnName))<br />
Mark.Append("?,")<br />
Next<br />
CreateString = CreateString.Remove(CreateString.Length - 2, 2)<br />
CreateString.Append(")")<br />
Columns = Columns.Remove(Columns.Length - 2, 2)<br />
Columns.Append(")")<br />
Mark = Mark.Remove(Mark.Length - 1, 1)<br />
Mark.Append(")")<br />
Using Command As New OleDb.OleDbCommand(CreateString.ToString, Connection)<br />
Command.ExecuteNonQuery()<br />
End Using<br />
Using Adapter As New OleDb.OleDbDataAdapter("SELECT * FROM [" & table.TableName & "$]", Connection)<br />
Using ExcelDataset As New DataSet<br />
Debug.WriteLine("ExcelDataset.Locale.Name = " & ExcelDataset.Locale.Name)<br />
Adapter.Fill(ExcelDataset, table.TableName)<br />
Adapter.InsertCommand = New OleDb.OleDbCommand("INSERT INTO [" & table.TableName & "] " & Columns.ToString & " VALUES " & Mark.ToString, Connection)<br />
For Each Column As DataColumn In table.Columns<br />
Select Case Column.DataType.Name<br />
Case "SByte", "Byte", "Int16", "Int32", "Int64", "Double", "Single" ', "Decimal"<br />
Adapter.InsertCommand.Parameters.Add("@" & OS(Column.ColumnName), OleDb.OleDbType.Numeric, 100, OS(Column.ColumnName))<br />
Case "Decimal"<br />
'BIG, BIG Warning about this one, Inserting a a double intead of a Decimal is done to fix a bug(?) in OleDB <br />
'when using another Language setting than en-US on the computer (decimal comma vs. decimal dot)<br />
'Changing Cultureinfo on the CurrentThread doesn't help<br />
<br />
Adapter.InsertCommand.Parameters.Add("@" & OS(Column.ColumnName), OleDb.OleDbType.Double, 100, OS(Column.ColumnName))<br />
<br />
'Here's my original to use with en-US if anyone is interested:<br />
'Dim myParameter As New OleDb.OleDbParameter("@" & OS(Column.ColumnName), OleDb.OleDbType.Decimal, 100, OS(Column.ColumnName))<br />
'myParameter.Precision = 8<br />
'myParameter.Scale = 4<br />
'Adapter.InsertCommand.Parameters.Add(myParameter)<br />
<br />
Case "Boolean"<br />
Adapter.InsertCommand.Parameters.Add("@" & OS(Column.ColumnName), OleDb.OleDbType.Boolean, 100, OS(Column.ColumnName))<br />
Case "Char", "String"<br />
Adapter.InsertCommand.Parameters.Add("@" & OS(Column.ColumnName), OleDb.OleDbType.Char, 65536, OS(Column.ColumnName))<br />
Case "DateTime"<br />
Adapter.InsertCommand.Parameters.Add("@" & OS(Column.ColumnName), OleDb.OleDbType.DBTimeStamp, 100, OS(Column.ColumnName))<br />
Case Else<br />
Adapter.InsertCommand.Parameters.Add("@" & OS(Column.ColumnName), OleDb.OleDbType.Char, 65536, OS(Column.ColumnName))<br />
End Select<br />
Next<br />
For Each Row As DataRow In table.Rows<br />
If Row.RowState <> DataRowState.Deleted Then<br />
Dim ExcelRow As DataRow = ExcelDataset.Tables(table.TableName).NewRow<br />
For i As Integer = 0 To table.Columns.Count - 1<br />
ExcelRow.Item(i) = Row.Item(i)<br />
Next<br />
ExcelDataset.Tables(table.TableName).Rows.Add(ExcelRow)<br />
End If<br />
Next<br />
Adapter.Update(ExcelDataset, table.TableName)<br />
End Using<br />
End Using<br />
Next<br />
End Using<br />
System.GC.Collect()<br />
End Sub
|
|
|
|
|
|
Don't double post. It's rude and against forum rules.
You've just asked this question. Why have you asked it again?
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
|
Hi
can u please help me to run following code.
___________________________________
declare @tablename varchar(20);
SET @tablename = 'table1';
select * from @tablename;
error: must declare @tablename
Thanks
Jayesh
|
|
|
|
|
Why do you want to do that in the first place?
If your table name is known only at runtime, you've to use dynamic sql
<br />
declare @tablename varchar(20), @sql nvarchar(256);<br />
SET @tablename = 'table1';<br />
set @sql = 'select * from ' + @tablename<br />
exec sp_executesql @sql<br />
SG
Cause is effect concealed. Effect is cause revealed.
|
|
|
|
|
actually i need query with dynamic table name for cursor where ur this code will not help me dear.
my problem is here..
declare @TABLENAME as varchar(50);
SET @TableAuto = 'temp_' + @LINE; (where @LINE is parameter)
DECLARE C1 CURSOR READ_ONLY
FOR SELECT *
FROM @TABLENAME
modified on Wednesday, April 9, 2008 4:18 PM
|
|
|
|
|
Is there anyone who knows about how to connect PROGRESS databse? How do I make connection string with ADO.NET? Please let me know.
|
|
|
|
|
Dong Kim wrote: Is there anyone who knows about how to connect PROGRESS databse?
Nope. I certainly don't.
Dong Kim wrote: How do I make connection string with ADO.NET?
See this page[^]
|
|
|
|
|
try this site[^]. It's a good one to reference for all types of connection strings.
|
|
|
|
|
Hi guys,
how do I insert into an oracle database table with a select statement from within mssql server2005 or 2000?
e.g
Oracle db: Table A id,msisdn,message
Msql server:Table A id, msisdn, message.
1 +476334566 Happy birhday
So I want to select this result from sql server database and insert into oracle Table A.
Thanks for you solutions.
|
|
|
|
|
Nasty
You need to set up a linked server from SQL to Oracle, this will give you access to the table.
This is a particularly nasty thing to have to do, the server name will then be hard coded into your procedure and will require modification when you promote the proc from dev/uat to production. (and I hate linked servers and I hate setting up Oracle and....)
Good Luck.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I have 2 columns with data type "Datetime"
i want to write a query which will return max of these 2 datetimes and also i want to consider the time part of the fields i.e hours and minutes, but all this without using stored procedures or function.
Can any one help please.
|
|
|
|
|
<br />
DECLARE @Dates TABLE (date1 DATETIME, date2 DATETIME)<br />
<br />
INSERT INTO @Dates (date1, date2) <br />
VALUES (DATEADD(d, -1, GETDATE()), DATEADD(m, -1, GETDATE()))<br />
INSERT INTO @Dates (date1, date2) <br />
VALUES (DATEADD(hh, -1, GETDATE()), DATEADD(hh, -2, GETDATE()))<br />
<br />
SELECT * FROM @Dates<br />
<br />
SELECT CASE WHEN date1 > date2 THEN date1 ELSE date2 END AS Date<br />
FROM @Dates<br />
|
|
|
|
|
thank you Very Much Mark for the solution
|
|
|
|
|
I have data of a start date, which I imported into SQL from an excell spreadsheet in which the date was an nvarchar type. So I imported it as smalldatetime, with the hopes that when I used a calender on my webpage it would use the same format and work beautifully! but it doesn't! for some reason, the calender uses the format: yyyy/mm/dd and the format I have it in my database is yyyy/dd/mm (and that's how it came with the import with the smalldatetime type). And I have fiddled with it all day and can't figure out how to get them to the same format as I can't get my SQL database to change my data to that format.
Am I doing something wrong or is this a silly problem that someone can help me with?
Thanks!
|
|
|
|
|
DATETIME and SMALLDATETIME fields will always have the same format. You need to convert it to a string and specify the output format you would like to use. The method for doing this will depend on the language you are using but any language should have a method for formatting date/time strings.
If you are using .NET for your web pages the DateTime class has overloaded ToString() methods for formatting the string. If you want the output of your SQL query to be formatted then use the CONVERT function:
.NET (c#)
<br />
myDateObject.ToString("yyyy/dd/MM");<br />
T-SQL
<br />
SELECT REPLACE(SUBSTRING(CONVERT(VARCHAR, GETDATE(), 120), 1, 10), '-', '/') AS Date<br />
T-SQL is less flexible, so you either need to concatenate calls to DATEPART (or YEAR, MONTH, DAY) or do something like the example above (SUBSTRING and REPLACE along with the 'style' argument of the CONVERT function).
|
|
|
|
|
Thanks, I will try that tomorrow morning. Will try the T-SQL script as I will only be using sql to do my coding.
appreciate the help
|
|
|
|
|
Substitute your date column for getdate() in this:
select replace(convert(varchar,getdate(),102),'.','/')
It gives you 2008/04/10
Bob
Ashfield Consultants Ltd
|
|
|
|
|
ok that's great for getting it into the correct format for todays date, but how do I get the last 20 000 rows of data to reflect the correct date format?!
|
|
|
|
|
I thought I was fairly clear, I said, replace the getdate() in this with your date column
so
select replace(convert(varchar,getdate(),102),'.','/')
becomes
select replace(convert(varchar,YOUR_DATE_COLUMN,102),'.','/')<br />
from YOUR_TABLE
Replacing YOUR_DATE_COLUMN and YOUR_TABLE with the names of your date column and your table name
Bob
Ashfield Consultants Ltd
|
|
|
|
|
oh sheez! ha, sorry man! didn't register in my head at all! bit of a blonde moment there!
Thanks so much!
|
|
|
|
|
kindly tel me how to handle Data Purging of previous month... We want to have the data of current month only...
|
|
|
|
|
I assume you have some kind of added date on your table(s), otherwise how will youi know the age of the data, but anyway:
delete tablea<br />
where addeddate < dateadd(month,-1,getdate())
This will delete anything over 1 month old today. Its easy enough to work on a month end/month start basis, I'll leave that for you to work out.
Also, if you have referential integrity between table you need to do the deletes in the correct order.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Ah, you cross posted. Please don't do that
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|