วันพฤหัสบดีที่ 11 เมษายน พ.ศ. 2556

เซฟไฟล์ใน Server


คือนกจะสั่งให้มันเซฟไฟล์ใน server ทีนี้เราจะหา Path ได้จาก
 
server.mappath()
 
ทีนี้เราก็สั่ง SaveAs ได้เลย
 
ขอบคุณเวปนี้ด้วยนะจ๊ะ http://www.narisa.com/forums/index.php?showtopic=17059&mode=threaded
 
ปล. นกนี่ไม่ค่อยจำคำสั่งเล้ย จะใช้ทีไรก็เพิ่ง Google ทุ๊กที อิอิ
 

คือนกจะเอาไฟล์ Excel เข้า SQL แล้วทีนี้ก็หามาหลายวิธีแล้ว ยากเหลือเกิน เลยเจอวิธีนี้ คือ สร้าง Stroe Procedure แล้วก็ส่ง Parameter ไป
แต่ปัญหาคือ Execute ไม่ผ่านเจ้าค่ะ ขึ้น Error มาตัวแดงแจ๋เลย
 
 
SQL Server blocked access to STATEMENT ‘OpenRowset/OpenDatasource’ of component ‘Ad Hoc Distributed Queries’ because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of ‘Ad Hoc Distributed Queries’ by using sp_configure. For more information about enabling ‘Ad Hoc Distributed Queries’, see "Surface Area Configuration" in SQL Server Books Online.
 
ทีนี้วิธีแก้ก็ตามนี้เลยนะค่ะ
ขั้นแรกเข้าไปที่ : Start > All Programs > Microsoft SQL Server 2005 > Confuguration Tool > SQL Server Serface Area Configuration
ขั้นที่สองก็ตามนี้ : Serface Area Configuration for the Feature
ขั้นที่สามก็แบบนี้ : คลิกเครื่องหมายถูกหน้า Enable Table RowSet
 
รับลองแก้ได้หายสนิทค่ะ ทีนี้ก็ลอง Execute กันอีกทีนึงนะค่ะ

Export Excel to SQL by Store Procedure


ALTER procedure [dbo].[SP_ImportExcelFile](
@Source varchar(1000)
, @SourceSheet varchar(100)
, @DestinationTable varchar(100))
as
declare @retval int
–check if file exists
EXEC master..xp_fileexist @Source, @retval output
if @retval = 0
begin
print ‘file does not exist.’
return
end
– check if worksheet exists and if not use Sheet1
if @SourceSheet is null or @SourceSheet = 
set @SourceSheet = ‘[Sheet1$]‘
else
set @SourceSheet = ‘[' + ltrim(rtrim(@SourceSheet)) + '$]‘
if @DestinationTable is null or @DestinationTable = 
set @DestinationTable = substring(@SourceSheet, 2, len(@SourceSheet)- 3) + convert(varchar, getdate(), 126)
exec(‘select * into [' + @DestinationTable + '] from openrowset(”Microsoft.Jet.OLEDB.4.0”, ”Excel 8.0;HDR=YES;Database=’+ @Source + ”’, ‘ + @SourceSheet + ‘)’)

ก๊อปโค้ดไปใช้กันได้เลยนะ แล้วเวลา Excecute ก็ Add Job แล้วเลือกแบบ T-SQL
แล้วก็ Excecute Store Procedure ที่เราเพิ่งสร้างไปได้เลยนะค่ะ โดยตัวอย่างก็ประมาณนี้หน่ะค่ะ
EXEC dbGF.dbo.SP_ImportExcelFile ‘C:\Documents and Settings\Administrator\Desktop\a.xls’,
     ’Sheet1′
     ,’a’
ทีนี้ก็เปลี่ยน Path Source File , Sheet , Table Name ได้ตามต้องการเลยนะค่ะ
แล้วที่สำคัญอย่าลืมเลือกที่ Database ที่เราต้องการด้วยนะค่ะ รวมถึงต้องมีตารางด้วยนะ

Can’t Execute Store Procedure


อาการเริ่มต้นก็มีอยู่ว่านกจะลอง Execute SP ที่นกสร้างไว้ แต่ดันเจอ Error ตัวนิดนึงแบบนี้
 
SQL Server blocked access to procedure ‘sys.xp_cmdshell’ of component ‘xp_cmdshell’ because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of ‘xp_cmdshell’ by using sp_configure. For more information about enabling ‘xp_cmdshell’, see "Surface Area Configuration" in SQL Server Books Online.
 
เค้าให้เราไป Enable มันซะที่ Surface Area Configuration
เมนูนี้ก็สามารถเข้าได้ที่ตัวโปรแกรม Program/MS SQL 2005/ Configuration Tool/Surface Area Configuration กันเลย
จากนั้นก็สั่งให้ xp_cmdshell มัน Enable แค่นี้ก็จะรัน SP ได้อย่างไร้กังวลกันแล้ว


ต้องขอขอบพระคุณเจ้าของด้วยนะเจ้าค่ะ http://www.mssqltips.com/tip.asp?tip1020

วันอังคารที่ 9 เมษายน พ.ศ. 2556

How to Execute DTS Package by ASP.NET


ตามหัวข้อกันไปเลยนะค่ะ เริ่มแรกคือนกมี DTS Package ที่สร้างจาก SQL อยู่แล้วแล้วทีนี้จะรันตัว Package กันอย่างไรโดย .NET
จริงๆ มันก็มีอยู่ 3 วิธี (น่าจะใช่นะถ้าจำไม่ผิด) คือ SSIS Control Package , SP (Store Procedure) , DOS
 
แต่วิธีที่นกจะใช้คือ ลำดับแรกนกสร้าง SP for Execute DTS Package ซะก่อน ลำดับต่อมาก็เขียน .NET ไปรันมันซะก็แค่นั้น
 
ลำดับแรกสร้าง SP เพื่อ Execute DTS Package
 
        CREATE procedure [dbo].[SP_ExecuteDTS](
      @DTSSource varchar(1000))
                AS  
      – Initialize command
      Declare @cmd varchar(1000)
      select  @cmd =  ‘dtexec /F "’ + @DTSSource +‘"’     
      exec master..xp_cmdshell @cmd  
                        
ลำดับต่อมาเขียน .NET เพื่อ Execute DTS Package ผ่าน SP
 
                  Imports System.Data
         Imports System.Data.SqlClient
          Sub runDTS()
Dim gDTS As String
Dim conn As SqlConnection
Dim comm As SqlCommand
Dim gServer$, gDatabase$, gUserID$, gPassword$
Dim qTrustedConnection As Boolean
gServer =
"crServer"
gDatabase =
"dbCR"
gUserID = "cr"
gPassword =
"crpassword"
qTrustedConnection =
False
Try
‘ นกเรียก Package จากคอนโทรล FileUpload
gDTS =
Me.FileUpload1.PostedFile.FileName
‘ เปิด Connection
conn =
New SqlConnection("Server=" & gServer & _
";Database=" & gDatabase & _
";User ID=" & gUserID & _
";Password=" & gPassword & _
";Trusted_Connection= " & qTrustedConnection & ";")
‘ เรียกใช้ SP_ExecuteDTS ที่เราสร้าง SP ไว้ใน SQL
comm =
New SqlCommand("SP_ExecuteDTS", conn)
comm.CommandType = CommandType.StoredProcedure
‘ ส่ง Parameter ไปซะว่า Package เราอยู่แถวไหน
comm.Parameters.Add(
"@DTSSource", SqlDbType.VarChar, 1000).Value = gDTS
conn.Open()
comm.ExecuteNonQuery()
conn.Close()
Me.Label1.Text = "Done"
Catch ex As Exception
me.Label1.Text=ex.Message.ToString
End Try
End Sub
เท่านี้ก็เรียกใช้งาน DTS Package ผ่าน ASP.NET ได้แว้วล่ะ
ปล. ก่อนจากบอกอีกนิดนึงคือนกเป็นคนไม่ค่อยจะจำอะไรได้ยาวๆ นานๆ เท่าไหร่ เลยมีเวปมาแนะนำที่ทำให้นกเขียนอันนี้ได้
งั้นวันนี้ก็แค่สร้างให้มัน Execute DTS Package อะไรๆ ก็ได้กันไปก่อนละกันนะ