473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

A String question

Tom
Hi,

I need to make the following sentence to be a string variable and how should
I do it?

server=SQL;database=SQLTest;user id=test;password=1234

private static string str= "server=SQL" + ";" + "database=SQLTest" + ";" +
"user id=test" + ";" + "password=1234";

It shows many errors.

Thanks


Nov 16 '05 #1
4 977
It's suggested to avoid string concatenations if you are concatenating 5 or
more strings. If you need to concatenate a lot of strings or a number of
strings that you can't predict you should use StringBuilder class. But in
this case i suggest you to take a look at the Concat method of the String
class.

HTH
"Tom" <To*@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...
Hi,

I need to make the following sentence to be a string variable and how
should
I do it?

server=SQL;database=SQLTest;user id=test;password=1234

private static string str= "server=SQL" + ";" + "database=SQLTest" + ";" +
"user id=test" + ";" + "password=1234";

It shows many errors.

Thanks

Nov 16 '05 #2
Hi Tom,

I don't see any immediate errors in your code, but why do you concatenate
when you can simply do

private static string str = "server=SQL;database=SQLTest;user
id=test;password=1234";

You don't say what errors you are getting so I can't help you there (I get
none).
As for connectionstrings, you may use this page for a selection of various
ones:

http://www.connectionstrings.com/
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
<"Felipe Garcia" <#>> wrote:
It's suggested to avoid string concatenations if you are concatenating 5 or
more strings. If you need to concatenate a lot of strings or a number of
strings that you can't predict you should use StringBuilder class. But in
this case i suggest you to take a look at the Concat method of the String
class.


String concatenations such as the one given are fine - if you're
concatenating several strings in one step, it's more effective to do
that than to use a StringBuilder. It's if you end up creating
intermediate strings that things become inefficient.

In the example given, in fact, the compiler does the concatenation in
advance, so it's just a simple string assignment.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Tom <To*@discussions.microsoft.com> wrote:
I need to make the following sentence to be a string variable and how should
I do it?

server=SQL;database=SQLTest;user id=test;password=1234

private static string str= "server=SQL" + ";" + "database=SQLTest" + ";" +
"user id=test" + ";" + "password=1234";

It shows many errors.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

The code as you've posted it is fine. For example:

using System;

class Test
{
private static string str= "server=SQL" + ";" +
"database=SQLTest" + ";" +
"user id=test" + ";" + "password=1234";

static void Main()
{
Console.WriteLine (str);
}
}

compiles and runs with no problems.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sparks | last post by:
I know this is a stupid question but I can't find the answer. Please tell me where to find reference to this so I can print it out and staple it to my head.. dim I as integer dim missedstr as...
5
by: John Baro | last post by:
I have a richtextbox which I want the "literal" rtf of. richtextbox.rtf returns {\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0 when i put this into a string I get...
5
by: Dave | last post by:
I'm receiving info from a com port into a string. I gradually process the string which constantly shortens it. The question is how long can a string be before I need to write some info to disk...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
39
by: sucaba.r | last post by:
I don't know if this is a unique problem, or I'm going about it the wrong way. I currently connect to one of our SQL servers via a priviliged account (by using RUNAS). Works with no problem. I...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.