Showing posts with label insert. Show all posts
Showing posts with label insert. Show all posts

Sunday, March 25, 2012

Connection Pooling.

I have created an instance of sqlconnection and using transaction i am executing 5 insert statements (Database - SQL Server 2005). I have the finally block where i call the dispose method of the transaction and close method of the connection object.I often get the below mentioned error:

Exception Type: System.InvalidOperationException

Message: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

TargetSite: System.Data.ProviderBase.DbConnectionInternal GetConnection(System.Data.Common.DbConnection)

Source: System.Data

How do i resolve the above issue?

do you a transaction.Commit()?

Put the code.

Regards.

PD:

this is a transaction example:

Code Snippet

using (dbcmd.Transaction = dbcon.BeginTransaction())

{

try

{

dbcmd.CommandText = string.Format(sql1, tablaDestino, sqlOrigen);

dbcmd.ExecuteNonQuery();

dbcmd.CommandText = string.Format(sql2, tablaDestino);

dbcmd.ExecuteNonQuery();

dbcmd.CommandText = string.Format(sql3, tablaDestino);

dbcmd.ExecuteNonQuery();

dbcmd.CommandText = string.Format(sql4, tablaDestino);

dbcmd.ExecuteNonQuery();

dbcmd.CommandText = string.Format(sql5, tablaDestino);

dbcmd.ExecuteNonQuery();

dbcmd.Transaction.Commit();

}

catch (DbException dbException)

{

dbcmd.Transaction.Rollback();

throw new PoolException("La transaci¢n de creaci¢n de pool ha fallado","CreatePool", 0, dbException);

}

}

|||

Yes, I am doing a commit. We are using DAAB SQLHelper class.

|||

You may try to increase the size of the connection pool. For example: Set Min Pool Size=10;Max Pool Size=1000 in your connection string

|||I have increased the connection pool size. min as 5 and max 100. still it gives me a problem.|||I've had simillar problem with the connection pooling. Have you made sure that the connection used is closed after you are finished with it.

I found loads of open connections within my code and closing them seams to have helped.

Colin
|||Yes i am closing the connection after using.|||

I don't think this has anything to do with connection pooling once the connection is open -- as soon as you open your connection, it's yours until you close the connection -- no other connection can use it until you close it.

Connection pooling doesn't utilise your physical connection for other logical connections while it's open -- it simply doesn't physically close connections when you close them and assigns those connections to new logical connections by client applications when required.

|||Note that you simply may not have enough connections in the pool initially. By default, the MaxPoolSize=100. Try increasing this by placing MaxPoolSize=500 in the connection string.

Connection Pooling.

I have created an instance of sqlconnection and using transaction i am executing 5 insert statements (Database - SQL Server 2005). I have the finally block where i call the dispose method of the transaction and close method of the connection object.I often get the below mentioned error:

Exception Type: System.InvalidOperationException

Message: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

TargetSite: System.Data.ProviderBase.DbConnectionInternal GetConnection(System.Data.Common.DbConnection)

Source: System.Data

How do i resolve the above issue?

do you a transaction.Commit()?

Put the code.

Regards.

PD:

this is a transaction example:

Code Snippet

using (dbcmd.Transaction = dbcon.BeginTransaction())

{

try

{

dbcmd.CommandText = string.Format(sql1, tablaDestino, sqlOrigen);

dbcmd.ExecuteNonQuery();

dbcmd.CommandText = string.Format(sql2, tablaDestino);

dbcmd.ExecuteNonQuery();

dbcmd.CommandText = string.Format(sql3, tablaDestino);

dbcmd.ExecuteNonQuery();

dbcmd.CommandText = string.Format(sql4, tablaDestino);

dbcmd.ExecuteNonQuery();

dbcmd.CommandText = string.Format(sql5, tablaDestino);

dbcmd.ExecuteNonQuery();

dbcmd.Transaction.Commit();

}

catch (DbException dbException)

{

dbcmd.Transaction.Rollback();

throw new PoolException("La transaci¢n de creaci¢n de pool ha fallado","CreatePool", 0, dbException);

}

}

|||

Yes, I am doing a commit. We are using DAAB SQLHelper class.

|||

You may try to increase the size of the connection pool. For example: Set Min Pool Size=10;Max Pool Size=1000 in your connection string

|||I have increased the connection pool size. min as 5 and max 100. still it gives me a problem.|||I've had simillar problem with the connection pooling. Have you made sure that the connection used is closed after you are finished with it.

I found loads of open connections within my code and closing them seams to have helped.

Colin
|||Yes i am closing the connection after using.|||

I don't think this has anything to do with connection pooling once the connection is open -- as soon as you open your connection, it's yours until you close the connection -- no other connection can use it until you close it.

Connection pooling doesn't utilise your physical connection for other logical connections while it's open -- it simply doesn't physically close connections when you close them and assigns those connections to new logical connections by client applications when required.

|||Note that you simply may not have enough connections in the pool initially. By default, the MaxPoolSize=100. Try increasing this by placing MaxPoolSize=500 in the connection string.

Sunday, March 11, 2012

Connection information in a table trigger?

Is it possible to log or insert into a table the connection information (Application & Login) from an table trigger?

We have tough problem where data in a particular table is getting 'wiped-out' (rows are getting set to all NULLs) and we are unable to correlate this with any particular piece of software. My hope is that we can write an table trigger that can log or create an row in a temp table or the like to allow us to track this down to the offending application so that we can finallly get rid of the problem entirely.

Thanks,

You really should be using Profiler for this kind of activity. It will be able to give you more information than a TRIGGER. Profiler will allow you to capture the entire query (or queries) that are being sent to the server.

A TRIGGER would only allow you to log parameter and system values -NOT the actual query.

|||

Agree with Arnie for the most part, you can use profiler for this most likely. You would want to get really granular and track statements inside procedures too. You might also filter on the name of the table. Profiler can be kind of noisy/picky, so it might take a few tries, but it is the greatest thing to have in a crisis Smile

As far as a trigger, you can get some of this information from sys.sysprocesses (or master.dbo.sysprocesses for 2000 and earlier.) You could join some of the values with the values in the inserted and deleted tables to see what is happening at a granular way. What I might do is to add a trigger that does:

if exists (select * from inserted where columnIDon'tWantSetToNull is null)

begin

raiserror ('DON''T DO THIS!',16,1)

rollback transaction

end

insert into log

select inserted.key, sysprocesses.columns
from inserted
join sysprocesses

on sysprocesses.spid = @.@.spid

If the operation was not in a transaction, you will get a log row, but your data will certainly not be hosed. If your application/process doesn't just ignore errors, you can track it down that way.

|||

Thank you both for your help. I'm not very good/handy with profiler, but I'll give it a try if I don't get anywhere with it, I'll try with an trigger on the table and the sysprocess table information.

George

|||

You definitely need to get good with profiler. In my opinion, it is the greatest thing about SQL Server, and for a person who has worked only with SQL Server for 15 years, that is saying something. Diagnosing problems with SQL Server is so much easier than pretty much any other programming tool, simply because I can see, immediately what the "heathen" user is trying to do to it and stop them.

Of course it has made it easy to simply force the DBA to prove that the database is not the culprit first since it is so easy, but that is another story Smile

|||

How do I see the parameter values sent in a parameterized query in Profiler?

George

|||Profiler will show you the entire query, with parameters placed in the correct locations.

Tuesday, February 14, 2012

Connecting to sql2005 using C#

Help... I am new to C# and .net and I am trying to build a insert page with a couple of drop down controls where I pull a categoryID and subcategoryID to populate my dropdown controls from a MS sql2005 express database. I am using a book that only shows how to build the script and access a access database and I am getting this error when trying to pull up the page:

Description:An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message:CS0246: The type or namespace name 'OleDbConnection' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 3: Line 4: <script runat="server" language="C#">Line 5: OleDbConnection objConn = newOleDbConnection(Line 6: "Server=SIMBA\\NETSDK;" +Line 7: "Database=btuniverse;" +


Source File: c:\Inetpub\wwwroot\_addnews.aspx Line: 5

Below is my code and I am not sure what the syntax needs to be when connecting to a ms sql datasource. Thanks in advanced :)

MYCODE:

<%@. Page Language="C#" MasterPageFile="~/main.master" Title="Untitled Page" %>


<script runat="server" language="C#">
OleDbConnection objConn = newOleDbConnection(
"Server=SIMBA\\NETSDK;" +
"Database=btuniverse;" +
"User ID=sa;Password=password");
OleDbCommand objCmd;
OleDbDataReader objRdr;

void Page_Load() {
if (!IsPostBack) {
objConn.Open();

objCmd = new OleDbCommand("SELECT * FROM dbo.tblNewsCategories", objConn);
objRdr = objCmd.ExecuteReader();
ddlCategory.DataSource = objRdr;
ddlCategory.DataValueField = "CategoryID";
ddlCategory.DataTextField = "CategoryName";
ddlCategory.DataBind();
objRdr.Close();

objCmd = new OleDbCommand("SELECT * FROM dbo.tblSubCategories", objConn);
objRdr = objCmd.Executereader();
ddlSubCategory.DataSource = objRdr;
ddlSubCategory.DataValueField = "SubCategoryID";
ddlSubCategory.DataTextField = "SubCategoryName";
ddlSubCategory.DataBind();
objRdr.Close();

objConn.Close();
}
}


</script>


<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<table>
<tr>
<td align="right">Title:</td>
<td align="left"><asp:TextBox ID="txtArticleTitle" CssClass="textbox" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtArticleTitle" ErrorMessage="*" runat="server" />
<br />
</td>

<!-- <asp:CompareValidator ID="cvArticleTitle" ControlToValidate="txtArticleTitle" Operator="DataTypeCheck" Type="String" ErrorMessage="No numbers allowed" />-->
</tr>
<tr>
<td align="right"><p>Article Category:</td><td align="left"><asp:DropDownList ID="ddlCategory" CssClass="dropdownmenu" runat="server" /></td>
</tr>
<tr>
<td align="right"><p>Article Sub Category:</td><td align="left"><asp:DropDownList ID="ddlSubCategory" runat="server" /></td>
</tr>
<tr><td align="right">News Article:</td><td align="left"><asp:TextBox ID="txtArticleDesc" CssClass="textbox" Columns="40" Rows="4" TextMode="MultiLine" runat="server" />
<asp:RequiredFieldValidator ID="rfvArticleDesc" ControlToValidate="txtArticleDesc" ErrorMessage="*" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>

<asp:Button ID="btnSubmit" CssClass="button" runat="server" Text="Submit" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
</asp:Content>

Use SQLDBConnection.
Why dont you check out the tutorials on these forums. They have good explanation with sample code too.|||thank you.

Now that I am able to query certail tables and create drop down menus i am getting an error when trying to insert my record:

Exception Details:System.Data.OleDb.OleDbException: Must declare the scalar variable "@.NewsID".

Source Error:

Line 50: objCmd.Parameters.Add("@.ArticleLink", txtArticleLink.Text);
Line 51: objConn.Open();
Line 52: objCmd.ExecuteNonQuery();
Line 53: objConn.Close();
Line 54: Response.Redirect("mynews.aspx");

HERE IS M CODE:
void SubmitNewsArticle(Object s, EventArgs e)
{
objCmd = new System.Data.OleDb.OleDbCommand(
"INSERT INTO tblNews (NewsID, ArticleDate, Title, " +
"CategoryID, SubCategoryID, ArticleDesc, ArticleLink) " +
"VALUES (@.NewsID, @.Title, @.CategoryID, " +
"@.SubCategoryID, @.ArticleDesc, @.ArticleLink)", objConn);
objCmd.Parameters.Add("@.NewsID", 1);
objCmd.Parameters.Add("@.ArticleDate", txtArticleDate.Text);
objCmd.Parameters.Add("@.Title", txtArticleTitle.Text);
objCmd.Parameters.Add("@.CategoryID",
ddlCategory.SelectedItem.Value);
objCmd.Parameters.Add("@.SubCategoryID",
ddlSubCategory.SelectedItem.Value);
objCmd.Parameters.Add("@.ArticleDesc", txtArticleDesc.Text);
objCmd.Parameters.Add("@.ArticleLink", txtArticleLink.Text);
objConn.Open();
objCmd.ExecuteNonQuery();
objConn.Close();
Response.Redirect("mynews.aspx");
}

what am I missing?? sorry for the posts :( so new to C# and .net -- I am a coldfusion web developer (1 year exp)

thanks again in advanced|||looks like you are still using OLEDB connection. What is your backend? SQL Server or Access/something else? Check ifthis articlehelps (its in VB.NET but you could get an idea of how to declare parameters and set the values).