|
Hi all,
Could you please help me how to access a database from the following config file(web config).
<configuration>
<sqlserver>SERVER_BMRD</sqlserver>
<sqldatabase>B</sqldatabase>
<sqluser>BD_user</sqluser>
<sqlpassword>^1^vtqhCBOwbVJc+ODAe2Sm7A==</sqlpassword>
<sqltrusted>False</sqltrusted>
</configuration>
How will i be able to connect to the above database?
For example :
SqlDataAdapter sda = new SqlDataAdapter("Stored_procedure", Connectionstring);
Can you give me how my connection string should look like?
Thank you so much
|
|
|
|
|
|
Thanks for your response Rob.
It was given to me as a database credentials to be able to connect to the database.
You can see that they dont use connection string ?so that is my problem
How can i connect to a database using those credentials?
Regards,
kibrom
modified on Monday, February 9, 2009 12:23 PM
|
|
|
|
|
OK, to my knowledge that won't be easy. Config files have to follow a certain format, and you define sections when you want to implement your own settings. Just placing individual bits in there like that will likely cause the ConfigurationManager to protest.
Your app.config file is invalid. As such, the best bet would be to bypass the ConfigurationManager altogether and process the file as any other xml file. One such way would be to use an XmlDocument and retrieve the settings using XPath.
Regards,
Rob Philpott.
|
|
|
|
|
Hi Rob ,
Thank you so much for your help.Sorry as i am new to this could you please give me a tip on how to use it with XMLReader or XMlDocument?
Thanks a lot.
|
|
|
|
|
Sure. I'm just about to leave work now, so I'll put something together in a couple of hours once home. A few things to check - this file is called web.config and resides in the usual place with your application?
Also, you absolutely cannot change the format of this file? A simple inclusion of appSettings would make this a breeze.
I'd create a class which reads this file and then exposes each setting you need as a public string property. If I remember correctly the ASP.NET world watches web.config and updates automatically - you wouldn't get that with this approach.
More later...
Regards,
Rob Philpott.
|
|
|
|
|
Hi Rob,
Your help is Much appreciated Rob.Yes please its a config file.But its not a webconfig file.For instance the above can be called admin.config file.
The file doesnt have app setting? we can still include appSettings.However they dont have it in this file.
Thanks again for your time.
|
|
|
|
|
The class below should do what you want, create a new instance passing in the config file as the parameter to the constructor.
public class DatabaseConfiguration
{
public readonly string SqlServer;
public readonly string Database;
public readonly string User;
public readonly string Password;
public readonly bool Trusted;
public DatabaseConfiguration(string path)
{
XmlDocument settings = new XmlDocument();
settings.Load(path);
SqlServer = settings.SelectSingleNode("/configuration/sqlserver").InnerText;
Database = settings.SelectSingleNode("/configuration/sqldatabase").InnerText;
User = settings.SelectSingleNode("/configuration/sqluser").InnerText;
Password = settings.SelectSingleNode("/configuration/sqlpassword").InnerText;
Trusted = settings.SelectSingleNode("/configuration/sqltrusted").InnerText.ToLower() == "true";
}
}
Regards,
Rob Philpott.
|
|
|
|
|
Hi Rob,
You are a champion,thanks a lot.I have done it like you said.
Sorry one more question, Can you give me an example on how to execute a stored procedure with the class.
Like for instance :-
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("Test", ???????????????);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
sda.SelectCommand.Parameters.Add("@timeInStart", SqlDbType.DateTime);
sda.SelectCommand.Parameters["@timeInStart"].Value = from;
sda.SelectCommand.Parameters.Add("@timeInEnd", SqlDbType.DateTime);
sda.SelectCommand.Parameters["@timeInEnd"].Value = to;
sda.Fill(ds);
????????- This is a place for connection string? How do i go about that?
Thanks a lot Rob
|
|
|
|
|
Well, your connection string should look like this (from connectionstrings.com):
Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;
So, add a new string property to the class and construct that string using what you have. That's what you want for ?????????.
eg:
public string ConnectionString
{
get
{
return string.Format("Server={0};Database={1};User ID={2};Password={3};Trusted_Connection={4};", SqlServer, Database, User, Password, Trusted ? "True" : "False");
}
}
Regards,
Rob Philpott.
|
|
|
|
|
Hi Rob,
Thanks very much.Can we connect to the database with out using the connection string?
Just with the class you have given me? Can you give me just an example.
Sorry i am taking you time.
Thanks Rob
|
|
|
|
|
Yeah, you could add some functionality to that class which returns a valid open connection, and just use that as the connection
Something like:
public SqlConnection CreateConnection()
{
SqlConnection connnection = new SqlConnection(ConnectionString);
connection.Open();
return connection;
}
Remember to use the connection in a using bloke or call its dispose when you are done with it.
Regards,
Rob Philpott.
|
|
|
|
|
Do you think this should work to execute a stored procedure.
SqlDataAdapter sda = new SqlDataAdapter("Stored_proce", DatabaseConfiguration.connectionstring);
or simply use
SqlDataAdapter sda = new SqlDataAdapter("Stored_proce", DatabaseConfiguration.createConnection
I hope this will definely sort me out.
Thanks a million Rob.
|
|
|
|
|
I think you can use the first approach. Personally I'd create a connection, then create a command, then attach the adapter to that but its just personal preference.
You can reuse your connection if doing lots of things that way.
Regards,
Rob Philpott.
|
|
|
|
|
Thank very much for your help Rob.I will keep you posted.
Many thanks
|
|
|
|
|
Hello everybody
I am a real beginner in C# programming language.
I have a binary file, with binary data (ok). I would like to find a specific string in the file and make calculation of the found values.
Example: In the binary file, I have : "#{'"éé&$$¨DATAFOUNDllolml"{MYDATA|||\$$^p^££"
How can I do to capture MYDATA, using DATAFOUND as a key entry.
In my whole file, after DATAFOUND I have 8 bytes and then the searched values (here it's MYDATA).
Any advice and help are welcome.
PS: I am new programmer and I am not from an English speaking country...So be comprehensive.
|
|
|
|
|
mamamiya_ wrote: Hello everybody
welcome to CodeProject
mamamiya_ wrote: I have a binary file
For File I/O discussions "binary" means the data in the file contains bytes with the value of ZERO. In that case using String library functions are problematic.
mamamiya_ wrote: How can I
You need to parse the file. If, as discussed above, the data is all string data then there are numerous methods such as Regular Expressions. However deciding the best method of parsing and searching the data requires a knowledge of the file format.
|
|
|
|
|
Finding the string is trivial (and I assume that your data begins immediately after the string), but how do you know where your data ends, or even how much data there is?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
The data are always 8 bytes long.
|
|
|
|
|
|
suni_dotnet wrote: my page postbacks many times and this button click occurs in between.
"in betweeen" what?
|
|
|
|
|
I can't figure out what you're trying to do, but since it's asp.net related you're asking in the wrong forum.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
I have written an application using FileSystemWatcher and put the Windows Service on our data server.
Now my question is, using FileSystemWatcher, is there a way to get the windows logon name that actually deleted the file? I realy want to know who deleted or created the files. Is this possible?
|
|
|
|
|
Depending on what Windows Server OS you have and how it's configured (NTFS I think) I believe Windows is capable of auditing the File System. You need to configure it of course.
|
|
|
|
|
Our data server is running Server 2008 x64.
So are you saying Windows has something built in instead of writing an app for it?
|
|
|
|