0

I am creating an app that creates the database when you start the program , and the first script that I make is this:

public void updateAttachment(String PID)
{
    String connection = Constants.localServerConnectionSQL.LocalServerConnectionSQLName;
    SqlConnection Connection = new SqlConnection();

    Connection = new SqlConnection(connection);
    Connection.Open();

    Connection.ChangeDatabase(PID);

    string script = @"CREATE PROCEDURE updateAttachment 
    @ATID TEXT, @NAME TEXT,@CPID TEXT,
    @CREATORID text,@B64CONTENT text,
    @FILESIZE int,@SERVEROFFLINE int,
    @ISFAVOURITE int,@LASTOPENDATE date 
    AS UPDATE dbo.ATTACHMENT 
       SET NAME = @NAME, ATID = @ATID, CPID = @CPID,
           CreatorID = @CREATORID,B64CONTENT = @B64CONTENT,
           FILESIZE = @FILESIZE,SERVEROFFLINE = @SERVEROFFLINE,
           ISFAVOURITE = @ISFAVOURITE,
           LASTOPENDATE = @LASTOPENDATE 
       WHERE ATID LIKE @ATID";
    SqlCommand command = new SqlCommand(script, Connection);

    try
    {
        command.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

And then I'm going to call the use of stored procedures using this method :

public void updateAttachments(String PID, AttachmentDownload attachment)
{
    using (SqlConnection conn = new SqlConnection(Constants.localServerConnectionSQL.LocalServerConnectionSQLName))
    {
        conn.Open();
        conn.ChangeDatabase(PID);
        SqlCommand cmd = new SqlCommand("dbo.updateAttachment", conn);

        cmd.CommandType = CommandType.StoredProcedure;


        cmd.Parameters.Add(new SqlParameter("@CPID", attachment.CPID));
        cmd.Parameters.Add(new SqlParameter("@NAME", attachment.Name));
        cmd.Parameters.Add(new SqlParameter("@ATID", attachment.ATID));
        cmd.Parameters.Add(new SqlParameter("@CREATORID", attachment.CreatorID));
        cmd.Parameters.Add(new SqlParameter("@B64CONTENT", attachment.B64Content));
        cmd.Parameters.Add(new SqlParameter("@FILESIZE", attachment.FileSize));
        cmd.Parameters.Add(new SqlParameter("@SERVEROFFLINE", attachment.ServeOffline));
        cmd.Parameters.Add(new SqlParameter("@ISFAVOURITE", attachment.IsFavourite));
        cmd.Parameters.Add(new SqlParameter("@LASTOPENDATE", attachment.LastOpenDate));

        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            log.Info("Errore di scrittura Store Procedure: insertMeetings - Errore.: " + ex.Message);
            throw ex;
        }       
    }
}

But when I launch the script , I do not by any kind of error but nevertheless I'm not updating the records ... where ATID equals attachment.ATID . Am I wrong sql server language ?

UPDATE i remove the quote WHERE ATID LIKE '@ATID' in WHERE ATID LIKE @ATID but i have this error:

The transaction log for database 'mydbname' is full due to 'ACTIVE_TRANSACTION'

how it solve ?

-- SOLVED / SOLUTION --

The script of creation database:

String str = "CREATE DATABASE [" + PID + "] ON PRIMARY (NAME = MyDatabase_Data, FILENAME = '" + Path + ".mdf', SIZE = 10MB, MAXSIZE = 15MB, FILEGROWTH = 10%) LOG ON (NAME = [" + PID + "Log_Log], FILENAME = '"+Path+ "Log.ldf', SIZE = 3MB, MAXSIZE = 10MB, FILEGROWTH = 10%);";

i have changed the script db creation from

"SIZE = 1MB, MAXSIZE = 5MB"

to

"SIZE = 3MB, MAXSIZE = 10MB" 

and now it work. the problem was log file space on disk.

Mr. Developer
  • 3,295
  • 7
  • 43
  • 110
  • I suggest you to use the Add method overload of the Parameters collection where you could specify exactly the type and the size of the parameters passed. In this way you have more control on all those TEXT parameters – Steve Apr 26 '16 at 12:20

1 Answers1

0

You don't need the closing quotes around your WHERE ATID LIKE '@ATID'". Remove the single quotes and you should be good to go

Simon Wilson
  • 9,929
  • 3
  • 27
  • 24
  • if i remove the quote i have this error: The transaction log for database 'mydbname' is full due to 'ACTIVE_TRANSACTION'. – Mr. Developer Apr 26 '16 at 12:13
  • If you tried Steve's suggestion take a quick look at [this](http://stackoverflow.com/questions/17674973/the-transaction-log-for-the-database-is-full) answer and see if any of the offered solutions work for you. – Simon Wilson Apr 26 '16 at 12:56
  • i have solved and i have update my question with solution...thanks all – Mr. Developer Apr 26 '16 at 12:59
  • Just noticed that the WHERE clause is comparing TEXT fields. You can't index a text field so you are executing a full table scan on each execution of the sproc. You might want to reconsider this strategy. – Simon Wilson Apr 26 '16 at 13:00