CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: exec what is that for within a sqlstament using with executenonequery?

  1. #1
    Join Date
    Aug 2015
    Posts
    59

    Question exec what is that for within a sqlstament using with executenonequery?

    Hello,

    to create a table for an mssql server, using ADO.net, I can make it with or without exec:
    Code:
     string sqlstament = "CREATE TABLE [dbo]. newTable" +
                    " ([RID][int] NOT NULL PRIMARY KEY CLUSTERED, [Contact][nvarchar](3) NULL," +
                     "[Company][nvarchar](40) NULL) ON [PRIMARY]";
    mySQlCommand.Commandtext = sqlstament;
    mySQlCommand.ExecuteNonQuery;
    or with 'exec',
    Code:
     string sqlstament = "EXEC ('CREATE TABLE [dbo]. newTable" +
                    " ([RID][int] NOT NULL PRIMARY KEY CLUSTERED, [Contact][nvarchar](3) NULL," +
                     "[Company][nvarchar](40) NULL) ON [PRIMARY]')";
    
    mySQlCommand.Commandtext = sqlstament;
    mySQlCommand.ExecuteNonQuery();
    both works fine.
    But I have no idea what I win if I use exec?

  2. #2
    Join Date
    Sep 2015
    Posts
    3

    Re: exec what is that for within a sqlstament using with executenonequery?

    Exec ist a keyword to construct the sql-statement with parameters, like here:
    "EXEC ('CREATE TABLE ' + @myTable + '( Yourfields')";

    In that case you the sqlcommand needs to have :
    myCmd.Parameters.AddWithValue("@myTable", SqlDbType.Text).Value = strtblName;

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)