|
Hi,
MahaKh wrote: how can i connect visual studio 2008 application to sql server 2008 using ODBC?
Here's an example that works for me (.NET 2.0, W7, SQL 2005)
using (OdbcConnection con = new OdbcConnection(
"DRIVER={SQL Server};SERVER=.;Trusted_connection=yes;DATABASE=AdventureWorks;"))
using (OdbcCommand cmd = con.CreateCommand())
{
con.Open();
cmd.CommandText = @"
SELECT FirstName
FROM Person.Contact
WHERE ContactID = 20";
string 结果 = (string)cmd.ExecuteScalar();
MessageBox.Show(String.Format(
"The FirstName of Person.Contact is '{0}'.", 结果));
}
..and this on top of that unit, between the other imports and the namespace-declaration;
using System.Data.Odbc;
And here's the manual[^]
I are Troll
|
|
|
|
|
thanks to all, but my requirement is to use OLEDB instead of ODBC....
|
|
|
|
|
MahaKh wrote: but my requirement is to use OLEDB instead of ODBC
The requirement should state that you should use a SqlConnection . Anyway, the difference between the code is relatively subtle;
using (OleDbConnection con = new OleDbConnection(
"Provider=SQLNCLI;Server=.;Database=AdventureWorks;Trusted_Connection=yes;"))
using (OleDbCommand cmd = con.CreateCommand())
{
con.Open();
cmd.CommandText = @"
SELECT FirstName
FROM Person.Contact
WHERE ContactID = 20";
string result = (string)cmd.ExecuteScalar();
MessageBox.Show(String.Format(
"The FirstName of Person.Contact is '{0}'.",
result
));
} Code tested and verified on W7, .NET 2.0, SQL2k5
I are Troll
|
|
|
|
|
I have noticed looking at MS source that they regularly do this for a class with only static methods instead of creating a static class:
public class ClassName
{
private ClassName()
{ }
public static void Method()
{
}
}
Is there any benifit to this that I'm missing over a static class?
|
|
|
|
|
Perhaps so the class can be inherited? just a guess
Don't vote my posts down just because you don't understand them - if you lack the superior intelligence that I possess then simply walk away
|
|
|
|
|
A class with a single private default constructor cannot be inherited. It will always need some way to instantiate the parent...
|
|
|
|
|
Yes well spotted. Thou I am not assuming the op sample code is 100% correct still
I may or may not be responsible for my own actions
|
|
|
|
|
Pre version 2? Backward compatibility?
|
|
|
|
|
Which sounds plausable, but why allow the class to be instanced (by making it non-static) when it can never be instantiated (because the constructor is private)? Backwards compatibility is going to break anyway, just in a much more awkward spot.
|
|
|
|
|
Private, schmivate; I can call a private constructor or anything else -- which is why they introduced static classes in .net 2.
|
|
|
|
|
COM Interop?
COM requires a public constructor without parameters, and there it is.
And isn't it possible to access class methods through an instance with COM? When I remember those times when I was VB programmer correctly, that was possible, but I am not sure.
|
|
|
|
|
I think this is laziness on the part of MS. If (when?) the class definition changes in the Framework, the documentarians won't have to root around in the code to make sure that every method is still static.
|
|
|
|
|
I don't think your argument is valid. Static methods in non-static classes have their own uses. I have given one such (trivial) example.
Cheers,
Karthik
|
|
|
|
|
I agree: I use static methods in non-static classes all the time. But the question was about non-static classes with nothing BUT static methods.
|
|
|
|
|
Yes, I noticed that later. So I also updated my answer to take that in to account (see EDIT in my answer)...
Cheers,
Karthik
|
|
|
|
|
You could use static methods if the functionality of the method is not dependent on the instance properties/methods. Consider the following class as an example. There is a static "Parse" method that takes in a string and return a new instance of "Data". This static function "does not" depend on any of the instance properties/methods of the Data class. But it just creates an instance of the Data class for you to use. So, any other class that wishes to create an instance of Data (which has a ',' seperated int's) can do so by calling Data.Parse(_stringValue) .
public class Data
{
private List<int> a;
public Data()
{
a = new List<int>();
}
public Data(int[] nums)
{
a = new List<int>();
}
public static Data Parse(string nums)
{
string[] _nums = nums.Split(",".ToCharArray());
List<int> iList = new List<int>();
foreach(string s in nums)
iList.Add(int32.Parse(s));
return new Data(iList.ToArray());
}
}
PS - This is a just a trivial example of the usage of static methods in a non-static class.
Edit - If there are only static methods in a non-static, it might be useless, but it could also be for futuristic reasons. They could add to this class more instance properties / methods in the future that would use these static methods.
Cheers,
Karthik
|
|
|
|
|
Hello,
I am new to wpf. I am creating a application in which I wanted to change my image dynamically with some URL.
I am using code like
TheBinding is property which returns URL.
I tried lot other ways. But nothing is working.
Thanks in advance.
|
|
|
|
|
If you're binding to the image from something like a VM, you need to read the image in first. The easiest way to do this is to use something like this:
public BitmapImage TheImage
{
get
{
return LoadImageFromSource();
}
}
private BitmapImage LoadImageFromSource()
{
BitmapImage image = new BitmapImage();
try
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = new Uri( FullPath, UriKind.Absolute );
image.EndInit();
}
catch{
return DependencyProperty.UnsetValue;
}
return image;
}
|
|
|
|
|
BTW - you should have asked this in the WPF forum.
|
|
|
|
|
Here only Feb 11 discussion details only showing. Please resolve.
|
|
|
|
|
|
Top of the forum, on your right, there's a filter (on the bar with noise tolerance). Set the "Date Filter" to "All" and hit "Update"
I are Troll
|
|
|
|
|
Use the site bugs forum for any such issues.
This is the .Net framework forum.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
|
|
|
|
|
I've been programming with .NET for half a year now.
It's all pretty great and fairly easy, but I can't help to think that some classes/components were made to have an extra shell around them.
Take the whole process of connecting and reading/writing data from and to a database.
At the very least we need a Connection object and a Command object and dependent on what you want to do you need a DataReader, DataAdapter, Parameters, CommandBuilders, etc... It seems to me that constantly creating all those objects, configuring them etc. seems like a lot of work. So it would seem to me that it is easier to create a class that requires some parameters in its constructor, has some methods and functions that manage all the just mentioned classes and simply gives a resultset using something like DBClass.Execute.
Basically it would then look something like:
Dim myClass as New DBClass(aString, commandType, connectionString)
myClass.AddParam(name, DBType.Int, value)
Dim result = myClass.Execute ' Or myClass.ExecuteASync!
' Do stuff with the result.
Another example of stuff that just screams for an extra layer around it is the whole printing process.
Have the PrintDialog, PrintPreviewDialog, PageSetupDialog, PrintDocument, maybe PrinterSettings, the whole event thing...
I just want to say:
Dim p as New PrintClass(document)
p.Print ' Or, once again, p.PrintASync! ;)
At this point, is anyone really disagreeing with me?
Or do you really agree and know some other stuff that just calls to be put away in a programmer friendly class?
It's an OO world.
|
|
|
|
|
There are many Database access frameworks. Look at Microsoft Data Access Library, or Entity Framework, or nHiberate, and many others.
You may need, or think you need, a wrapper for the printer functions in your application, but other many not.
IMO, six months of experience is not enough for you fully understand the concepts of OOD and be able to make such generalizations.
I know the language. I've read a book. - _Madmatt
|
|
|
|