Click here to Skip to main content
15,903,033 members
Home / Discussions / C#
   

C#

 
GeneralRe: UDP send /recieve problem... well mainly recieve Pin
Alberto Bar-Noy14-Nov-11 5:25
Alberto Bar-Noy14-Nov-11 5:25 
GeneralRe: UDP send /recieve problem... well mainly recieve Pin
Rob Philpott14-Nov-11 5:34
Rob Philpott14-Nov-11 5:34 
GeneralRe: UDP send /recieve problem... well mainly recieve Pin
Alberto Bar-Noy14-Nov-11 5:36
Alberto Bar-Noy14-Nov-11 5:36 
GeneralRe: UDP send /recieve problem... well mainly recieve Pin
Rob Philpott14-Nov-11 5:46
Rob Philpott14-Nov-11 5:46 
AnswerRe: UDP send /recieve problem... well mainly recieve Pin
Luc Pattyn14-Nov-11 5:35
sitebuilderLuc Pattyn14-Nov-11 5:35 
GeneralRe: UDP send /recieve problem... well mainly recieve Pin
Alberto Bar-Noy14-Nov-11 5:36
Alberto Bar-Noy14-Nov-11 5:36 
AnswerRe: UDP send /recieve problem... well mainly recieve Pin
Alberto Bar-Noy14-Nov-11 5:40
Alberto Bar-Noy14-Nov-11 5:40 
Questiondistribute stored procedure files Pin
Marcel Vreuls (www.agentbase.nl)14-Nov-11 2:30
Marcel Vreuls (www.agentbase.nl)14-Nov-11 2:30 
Hi All,

I have a winform application connected with a sql server 2005/2008 database. Now i have about 200 stored procedures which i would like to distribute with the application and execute as soon as the user creates a new datbase.

i do not want to make use of the specific dll like sqldmo and sqlserver.management, etc.. I want it to have it as clean as possible.

Below the function i use for testing ( forget the fixed file pathSmile | :) ). Creating datbase and tables are no problem. However creation of the storedprocedure returns errors.

************** Exception Text **************
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'TO'.
Incorrect syntax near the keyword 'TO'.
Incorrect syntax near '0'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at AgentBase.Business.clsDataModel.fncCreateNewDatabase(String DBNAME, String FILELOCATION) in C:\AgentBase.2010\AgentBase.Business\clsDataModel.cs:line 91
   at AgentBase.Gui.usxConnections.butCreateDB_Click(Object sender, EventArgs e) in C:\AgentBase.2010\AgentBase.Gui\usxConnections.cs:line 293
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


Any suggestions, tips, tricks.


C#
using System;
using System.IO;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AgentBase.Datalayer;
public static void fncCreateNewDatabase(string DBNAME, string FILELOCATION )
		{
		
			SqlConnection myConn = new SqlConnection(Globals.ConnectionString);		  

			string str = "CREATE DATABASE " + DBNAME + " ON PRIMARY " +
				"(NAME = " + DBNAME + "_Data, " +
				"FILENAME = '" + FILELOCATION + "\\DATA\\" + DBNAME + "_Data.mdf', " +
				"SIZE = 10MB, MAXSIZE = 10000MB, FILEGROWTH = 10%) " +
				"LOG ON (NAME = " + DBNAME + "_Log, " +
				"FILENAME = '" + FILELOCATION + "\\DATA\\" + DBNAME + "_Log.ldf', " +
				"SIZE = 5MB, " +
				"MAXSIZE = 50000MB, " +
				"FILEGROWTH = 10%)";

            string myServer = myConn.DataSource.ToString();

            SqlCommand myCommand = new SqlCommand(str, myConn);
			myConn.Open();
			myCommand.ExecuteNonQuery();
           
            myConn.Close();
            myConn = new SqlConnection("Server=" + myServer + ";Integrated security=SSPI;database=" + DBNAME + "");
            myConn.Open();
            SqlCommand cmd = new SqlCommand();
            //SqlDataReader reader;
            string sourceDir = "C:\\AgentBase.2010\\AgentBase\\bin\\Debug\\database\\";
            string[] fileEntries = Directory.GetFiles(sourceDir);
            foreach (string fileName in fileEntries)
            {

                // do something with fileName
                //FileInfo file = new FileInfo("C:\\AgentBase.2010\\AgentBase\\bin\\Debug\\database\\CreateDBStructure.sql");
                FileInfo file = new FileInfo(fileName.ToString());
                string script = file.OpenText().ReadToEnd();

                cmd.CommandText = script;
                //cmd.CommandType = System.Data.CommandType.Text;            //cmd.CommandType = cmd.CommandType.Text;
                cmd.Connection = myConn;
                cmd.ExecuteNonQuery();
			
            }
"




SQL
IF exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sptblPersonGetAll]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
BEGIN
   PRINT 'Dropping procedure sptblPersonGetAll ...'
   DROP PROCEDURE [dbo].[sptblPersonGetAll]
END
GO
PRINT 'Creating procedure sptblPersonGetAll ...'
GO


CREATE PROCEDURE [dbo].[sptblPersonGetAll]
(@PE_ID int
,@PE_RE_ID bigint
,@PE_TYPE bigint
,@PE_LASTNAME nvarchar(50)
,@PE_FIRSTNAME nvarchar(50)
,@PE_INITIALS nvarchar(50)
,@PE_CONFIRMATIONS bit
,@PE_INVOICES bit
,@PE_NEWORDER bit
)
AS
/* *********************************************************************************
**   Database:    REZON4V9
**   Project:     REZON4V9
**
**   Name:        [sptblPersonGetAll]
**   Decription:  Gets all the records in the '[tblPerson]' that match
**                the specified critieria. 
**
**   Remarks:     Any parameters passed as NULL will not affect the records returned.
**                They will effectively be ignored in the WHERE clause.
**
**   Called From: daltblPerson 
**
************************************************************************************
**                     CHANGE HISTORY
**  Change No:   Date:          Author:   Description:
**  _________    ___________    ______    ______________________________________
**     001       2/4/2011     V        Created.                                
**
** ****************************************************************************** */

SET NOCOUNT ON
SET XACT_ABORT OFF -- Allow procedure to continue after error

DECLARE @ProcName    varchar(30)       -- Name of this procedure
DECLARE @Error       integer           -- Local variable to capture the error code.
DECLARE @RowCnt      integer           -- Number of rows returned 
DECLARE @ErrMsg      nvarchar(1000)    -- Error message to return
DECLARE @Parameters  nvarchar(4000)    -- String representing parameters



-- *******************
-- Set local variables
-- *******************
SELECT @ProcName  = object_name(@@procid)
     , @Error     = 0
     , @RowCnt    = 0



-- *****************************************
-- Parameter string. Used for error handling
-- *****************************************
SET @Parameters = '' 
                + ' PE_ID:' + COALESCE(CONVERT(VARCHAR, @PE_ID), 'NULL')
                + ' PE_RE_ID:' + COALESCE(CONVERT(VARCHAR, @PE_RE_ID), 'NULL')
                + ' PE_TYPE:' + COALESCE(CONVERT(VARCHAR, @PE_TYPE), 'NULL')
                + ' PE_LASTNAME:' + COALESCE(CONVERT(VARCHAR, @PE_LASTNAME), 'NULL')
                + ' PE_FIRSTNAME:' + COALESCE(CONVERT(VARCHAR, @PE_FIRSTNAME), 'NULL')
                + ' PE_INITIALS:' + COALESCE(CONVERT(VARCHAR, @PE_INITIALS), 'NULL')
                + ' PE_CONFIRMATIONS:' + COALESCE(CONVERT(VARCHAR, @PE_CONFIRMATIONS), 'NULL')
                + ' PE_INVOICES:' + COALESCE(CONVERT(VARCHAR, @PE_INVOICES), 'NULL')
                + ' PE_NEWORDER:' + COALESCE(CONVERT(VARCHAR, @PE_NEWORDER), 'NULL')



-- ************
-- Get the data
-- ************
SELECT [PE_ID]
      ,[PE_RE_ID]
      ,[PE_TYPE]
      ,[PE_LASTNAME]
      ,[PE_FIRSTNAME]
      ,[PE_INITIALS]
      ,[PE_SEX]
      ,[PE_FUNCTION]
      ,[PE_PHONE]
      ,[PE_FAX]
      ,[PE_MOBILE]
      ,[PE_WEBSITE]
      ,[PE_EMAIL]
      ,[PE_SKYPE]
      ,[PE_LINKEDIN]
      ,[PE_FACEBOOK]
      ,[PE_BIRTHDAY]
      ,[PE_MEMO]
      ,[IS_DATECREATED]
      ,[IS_WHO]
      ,[IS_DATECHANGED]
      ,[PE_CONFIRMATIONS]
      ,[PE_INVOICES]
      ,[PE_NEWORDER]
      ,[PE_CONTACTFOR]
      ,[PE_SPEAKSLANGUAGE]
  FROM [dbo].[tblPerson]
 WHERE (([PE_ID] = @PE_ID) OR (@PE_ID IS NULL))
   AND (([PE_RE_ID] = @PE_RE_ID) OR (@PE_RE_ID IS NULL))
   AND (([PE_TYPE] = @PE_TYPE) OR (@PE_TYPE IS NULL))
   AND (([PE_LASTNAME] LIKE @PE_LASTNAME + '%') OR (@PE_LASTNAME IS NULL))
   AND (([PE_FIRSTNAME] = @PE_FIRSTNAME) OR (@PE_FIRSTNAME IS NULL))
   AND (([PE_INITIALS] = @PE_INITIALS) OR (@PE_INITIALS IS NULL))
   AND (([PE_CONFIRMATIONS] = @PE_CONFIRMATIONS) OR (@PE_CONFIRMATIONS IS NULL))
   AND (([PE_INVOICES] = @PE_INVOICES) OR (@PE_INVOICES IS NULL))
   AND (([PE_NEWORDER] = @PE_NEWORDER) OR (@PE_NEWORDER IS NULL))

SELECT @Error  = @@Error
     , @RowCnt = @@RowCount 
IF (@Error != 0)
BEGIN
   SELECT @ErrMsg = 'Error getting records. Parameters' + @Parameters
   GOTO ENDERROR
END



-- **************
-- Error Handling
-- **************
GOTO ENDOK

ENDERROR:
   BEGIN
      EXEC spErrorLogInsert 'DB Layer', @ProcName, @Error, @ErrMsg
      RAISERROR 25000 @ErrMsg
      RETURN 1
   END

ENDOK:
   RETURN 0
GO



IF ( object_id('dbo.sptblPersonGetAll') IS NOT NULL )
BEGIN
   PRINT 'Success: Created procedure sptblPersonGetAll.'
END
IF ( object_id('dbo.sptblPersonGetAll') IS NULL )
BEGIN
   PRINT 'ERROR: sptblPersonGetAll was not created successfully.'
END
GO

Kind regards,

Marcel Vreuls

AgentBase

<<A good idea can change your life>>

AnswerRe: distribute stored procedure files Pin
SilimSayo14-Nov-11 10:42
SilimSayo14-Nov-11 10:42 
QuestionMouse move (hover) over an Image with Graphics drawings Pin
eyalbi00714-Nov-11 2:12
eyalbi00714-Nov-11 2:12 
AnswerRe: Mouse move (hover) over an Image with Graphics drawings Pin
Luc Pattyn14-Nov-11 2:43
sitebuilderLuc Pattyn14-Nov-11 2:43 
QuestionClass for Serail Port Comms Pin
KeithF14-Nov-11 1:24
KeithF14-Nov-11 1:24 
AnswerRe: Class for Serail Port Comms Pin
Luc Pattyn14-Nov-11 1:48
sitebuilderLuc Pattyn14-Nov-11 1:48 
GeneralRe: Class for Serail Port Comms Pin
KeithF14-Nov-11 4:24
KeithF14-Nov-11 4:24 
AnswerRe: Class for Serail Port Comms Pin
Luc Pattyn14-Nov-11 5:06
sitebuilderLuc Pattyn14-Nov-11 5:06 
GeneralRe: Class for Serail Port Comms Pin
KeithF16-Nov-11 4:06
KeithF16-Nov-11 4:06 
AnswerRe: Class for Serail Port Comms Pin
Luc Pattyn16-Nov-11 16:02
sitebuilderLuc Pattyn16-Nov-11 16:02 
QuestionIterate all child forms and saving all reports to one excel. Pin
pinifg14-Nov-11 1:18
pinifg14-Nov-11 1:18 
AnswerRe: Iterate all child forms and saving all reports to one excel. Pin
Blue_Boy14-Nov-11 1:26
Blue_Boy14-Nov-11 1:26 
GeneralRe: Iterate all child forms and saving all reports to one excel. Pin
pinifg14-Nov-11 2:45
pinifg14-Nov-11 2:45 
AnswerRe: Iterate all child forms and saving all reports to one excel. Pin
DaveyM6914-Nov-11 1:45
professionalDaveyM6914-Nov-11 1:45 
QuestionConvert To English To Gujarati Pin
ramesh dabhi13-Nov-11 22:38
ramesh dabhi13-Nov-11 22:38 
AnswerRe: Convert To English To Gujarati Pin
Keith Barrow13-Nov-11 23:20
professionalKeith Barrow13-Nov-11 23:20 
AnswerRe: Convert To English To Gujarati Pin
Abhinav S14-Nov-11 1:42
Abhinav S14-Nov-11 1:42 
AnswerRe: Convert To English To Gujarati Pin
thatraja14-Nov-11 1:57
professionalthatraja14-Nov-11 1:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.