|
Yes.
I instantiate and dispose the connection only once, but I open and close it for each operation.
|
|
|
|
|
Define your connection at the start, then for each operation open then close the connection once the operation is complete.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
Richard.Berry100 wrote:
Is it bad practice to open a connection when the application starts, to avoid delays caused by opening the connection each time a query is executed?
Normal MS stuff uses a connection pool. The first time you create the connection it is opened. After that it gets it from the pool. It is already open and it stays open. It never closes while the application runs. That is the default behavior of the connection pool.
Your suggestion is doing nothing more than priming the pump to make it appear later that your application is more responsive.
So
1. There is nothing wrong with doing that.
2. It is unlikely to have any impact on the database server nor the architecture
3. It is a perfectly reasonable thing to do.
Richard.Berry100 wrote: or will SQL server drop it if it is unused for some time?
No SQL Server will not close idle connections. That itself is actually a problem because it means that if a client connection crashes (client fails or network fails) that the connection just remains there forever. One either must set up a process to clean them or at least look for them every once in a while.
|
|
|
|
|
It is still preferable to use Open to get the connection from the pool and Close to return it.
|
|
|
|
|
PIEBALDconsult wrote:
It is still preferable to use Open to get the
connection from the pool and Close to return it.
I would say it is the only way.
|
|
|
|
|
Yip, agreed, see the reply I posted to my own question with speed tests on various approaches, and reference to Eddy's link - Thanks!
|
|
|
|
|
 Hi Guys - thanks to all for the responses.
I think the link Eddy posted puts a lot into perspective, in that closing the connection does not really kill it, it leaves it in the pool, and unless there is something in the connection that changes, like accessing a different database, or using a different User ID the same connection from that pool is used again.
I tried inserting 10000 records (on server on local machine) in three different ways.
The difference was only about 100ms
private void cmdInsert_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
string cnStr = "Data Source=" + Server + ";Initial Catalog=" + Database + ";User Id=" + DBUser + "; Password=" + DBPass + ";";
string strSql = "Insert INTO dbo.SpeedTest (Mydata) VALUES ('Hello')";
sw.Start();
using (System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(cnStr))
{
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(strSql, cn);
for (int i = 0; i < 10000; i++)
{
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}
sw.Stop();
this.txtResults.Text = sw.ElapsedMilliseconds.ToString();
}
private void cmdInsert_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
string cnStr = "Data Source=" + Server + ";Initial Catalog=" + Database + ";User Id=" + DBUser + "; Password=" + DBPass + ";";
string strSql = "Insert INTO dbo.SpeedTest (Mydata) VALUES ('Hello')";
sw.Start();
using (System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(cnStr))
{
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(strSql, cn);
cn.Open();
for (int i = 0; i < 10000; i++)
{
cmd.ExecuteNonQuery();
}
cn.Close();
}
sw.Stop();
this.txtResults.Text = sw.ElapsedMilliseconds.ToString();
}
private void cmdInsert_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
string cnStr = "Data Source=" + Server + ";Initial Catalog=" + Database + ";User Id=" + DBUser + "; Password=" + DBPass + ";";
string strSql = "Insert INTO dbo.SpeedTest (Mydata) VALUES ('Hello')";
sw.Start();
for (int i = 0; i < 10000; i++)
{
using (System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(cnStr))
{
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(strSql, cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}
sw.Stop();
this.txtResults.Text = sw.ElapsedMilliseconds.ToString();
}
|
|
|
|
|
Hi all,
How do you recursively remove the last character from a string in Oracle e.g for string 'HPN12345', I would like to build the following:
HPN12345
HPN1234
HPN123
HPN12
HPN1
HPN
I know how to do this in Oracle 11 using a recursive common table expression (CTE), but I need to implement this in Oracle 10g (without using a user function).
Thanks
|
|
|
|
|
A while loop based on the length would work.
|
|
|
|
|
Try this one... Change value of MIN_DEPTH and CHAR_STR as required.
SELECT SUBSTR(A.CHAR_STR, 0, LENGTH(A.CHAR_STR) - LEVEL + 1) OUTPUT
FROM (SELECT 'HPN12345' CHAR_STR, 3 MIN_DEPTH FROM DUAL) A
CONNECT BY LENGTH(A.CHAR_STR) - LEVEL + 1 >= A.MIN_DEPTH;
Thanks & Regards,
Niral Soni
|
|
|
|
|
Thanks Niral.. Your code was very helpful. It helped point me in the right direction.
Much appreciated!
|
|
|
|
|
i have installed windows application in sql server 2008,it has been installed but when
i click the icon in desktop error getting is [DBNETLIB][CONNECTION OPEN(connect()] SPECIFIED SQL SERVER NOT FOUND.How can i over come this error.
Ramesh
|
|
|
|
|
I doubt you have installed a windows application into SQL Server 2008.
Member 9999112 wrote: How can i over come this error.
By actually providing real information.
First this site is for the people who create applications not those who use them. So if you have an application that you got from somewhere then you need to go where you got it and ask there. This presumes of course that you already read all of the documentation that comes with the application.
But if this is an application that your wrote yourself then you need to provide some more detail about the code that is failing.
|
|
|
|
|
Hi All,
I am getting conversion error when i try to execute the following query... please help i need this scenario to be fixed for another business logic
Conversion failed when converting the varchar value '12r' to data type int.
declare @t table
(
val varchar(10)
)
insert into @t
select '1' union
select '2' union
select '3' union
select '4' union
select 'et' union
select '4' union
select '6' union
select '7' union
select '8' union
select '9' union
select '12r'
select * from @t where cast(val as int) > 3
|
|
|
|
|
12r cannot be converted because it is not a number.
Use the best guess
|
|
|
|
|
Try this:
select *
from @t
where
cast((case when isnumeric(val) = 1 then val else 0 end) as int) > 3
Edit -this will turn the 12r into a zero as it is not a valid integer.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
This question really does ask for a disparaging response.
If you can't work out that declaring a field of type int and trying to stuff text into it is going to fail and then cannot understand the very clear error message you should stop now and get an education.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Perhaps JavaScript could cast '12r' into an int with a value of 12, but many other programming/scripting languages won't do so.
|
|
|
|
|
Bernhard Hiller wrote: JavaScript could cast '12r' into an int
It does, how extraordinary, no wonder I loathe the script language.
There is certain crappy banking software that will allow the traders to enter 12.65m or 328k as a trade values, WTF!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Mycroft Holmes wrote: There is certain crappy banking software that will allow the traders to enter 12.65m or 328k as a trade values, WTF!
Considering how close K and M are on a qwerty keyboard
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
Mycroft, I see original poster did not respond, but really curious - what does J-script convert '12r' to???
|
|
|
|
|
Richard.Berry100 wrote: what does J-script convert '12r' to???
Dammed if I know, as a Silverlight dev I don't have to get my hands grubby by rabbiting around in script languages.
The r probably stands for a currency sign, I image the conversion just drops the non numeric characters and turn the rest to an integer.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
SELECT
T1.ROW_ID,
T1.CREATED_BY,
T1.LAST_UPD_BY,
T1.NAME,
T1.PRTNR_FLG,
T1.CMPT_FLG,
T1.EMP_COUNT,
T1.BASE_CURCY_CD,
T1.CUST_STAT_CD,
T1.DOM_ULT_DUNS_NUM,
T1.DUNS_NUM,
T1.EAI_ERROR_TEXT,
T1.GLBLULT_DUNS_NUM,
T1.MAIN_PH_NUM,
T1.OU_NUM,
T1.PAR_DUNS_NUM,
T1.REGION,
T1.URL,
T1.X_DO_CONTACT_FLAG,
T1.X_DOMESTIC_NAME,
T1.X_GU_NAME,
T1.X_MARKET,
T1.X_MARKET_SEGMENT,
T1.X_NAME_IN_LOCAL,
T1.X_ONECODE_STATUS,
T1.X_PARENT_NAME,
T1.X_VENDOR_FLAG,
T1.X_GU_COUNTRY,
T1.X_INTERMEDIARY_FLAG,
T1.X_ONECODE_NAME,
T1.X_NUM_OF_EMP_EDITABLE,
T1.X_DOMESTIC_COUNTRY,
T1.X_HQPARENT_COUNTRY,
T1.X_AGU_FLG,
T1.X_ASSOC_MHGOLD,
T1.X_DB_TOTAL_ASSET_USD,
T1.X_DC_TOTAL_ASSET_USD,
T1.X_MDN_GOC_MESSAGE,
T1.X_MDN_GOC_SUBMIT_STATUS,
T1.X_CLIENT_TYPE,
T1.X_INT_LEV_TYPE,
T1.X_INT_REL_MGR_ID_MDN,
T1.X_GLOBAL_REL_MGR_ID_MDN,
T1.X_DNB_NAME_MDN,
T1.X_DNB_TRADE_NAME_MDN,
T1.X_DMG_COMMENT_MDN,
T1.X_MDN_GOC_DUPLICATE_CODE,
T1.X_MDN_GOC_DUPLICATE_NAME,
T1.X_MDN_GOC_INACTIVATION_DESC,
T1.X_MDN_GOC_REJECT_DESC,
T1.X_ACTIVE_DRMGLOBAL_MDN,
T1.X_ACTIVE_DRMINTER_MDN,
T1.X_ASSOC_ATTGOLD,
T1.X_AUTO_MERGE_DATE,
T1.X_AUTO_MERGE_FLG,
T1.X_AUTO_MERGE_STATUS_MDN,
T1.X_MDN_GOC_DUPLICATE_CODE_FLAG,
T1.X_MDN_GOC_DUPLICATE_DATE,
T1.X_MDN_GOC_SURVIVING_CODE,
T1.X_DNB_GU_ANNUAL_REVN,
T4.ATTRIB_04,
T4.X_REL_CLIENT_DESC,
T4.ATTRIB_34,
T5.PR_EMP_ID,
--T6.DESC_TEXT as Primary_Industry_Category__c,
--T6.SIC as Primary_SIC_Code__c,
--T6.NAME as Primary_SIC_Description__c,
C.X_OFFICE_CODE,
C.X_REGION,
T1.PR_INDUST_ID
FROM
siebel.S_ORG_EXT T1,
siebel.S_ORG_EXT_X T4,
siebel.S_POSTN T5,
siebel.s_contact C--,
--siebel.S_INDUST T6
--
where
T1.X_CLIENT_TYPE is NOT NULL
and T1.CUST_STAT_CD in('Active','Inactive','Pending - Workflow Wizard')
AND C.EMP_FLG(+)='Y'
and T4.ROW_ID(+)=T1.ROW_ID
and T5.ROW_ID(+)=T1.PR_POSTN_ID
--and T6.ROW_ID(+)=T1.PR_INDUST_ID
and C.ROW_ID=T5.PR_EMP_ID
|
|
|
|
|
Here you go you can get forked[^]!
Seriously do you expect someone to rewrite your query for you! You do not state your database, from the joins I assume oracle but you have given absolutely zero information!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
There's nothing to rewrite. You select columns and join, that's it - it's as dressed down as it can get. If speed is an issue, consider partitioning your table and verifying the indexes.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|