Alright world, I have a problem. No, this is not "homework"
I have code that should do two things:
1) Read the list of artists from the database
2) Find all the tracks written by this artist and save them later
What am I doing:
1) SELECT ArtistID FROM artists gets a list of my artists. I create it in a List<string> type using a DataReader , etc. It works great. 2) SELECT count(*) as track_numbers from tracks WHERE ArtistID = @ArtistID does not work so well
My problem is :
The second select statement will succeed if I manually specify ArtistID (i.e.: SELECT count(*) as track_numbers FROM Tracks WHERE ArtistID = 0 works), but if I repeat through my list, it will always return null results.
I am using Connector / Net 6.3.6, Visual Studio 2010 (all updates), .NET 4.0 Client Profile, MySQL Community Server 5.5.9 x64, Windows 7 Home Premium x64
I tried:
- Using string concatenation to create my query string using artid file
- casting artistid as a string, then int, and then vice versa
- redefinition, where parameters are defined, entered value, command text
The corresponding (I think) code below:
List<string> list = new List<string>(); Hashtable table = new Hashtable(); DbProviderFactory factory = DbProviderFactories.GetFactory(setting.ProviderName); using (DbConnection conn = factory.CreateConnection()) { conn.ConnectionString = setting.ConnectionString; conn.Open(); using (DbCommand cmd = conn.CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT ArtistID FROM artists;"; using (DbDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { list.Add(reader["ArtistID"].ToString()); //this works, and I can iterate no problem through this list } } } using (DbCommand cmd = conn.CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT count(*) as counts FROM tracks WHERE ArtistID = @ArtistID;"; //cmd.CommandText = "SELECT count(*) as counts FROM tracks WHERE ArtistID = 0;"; <-- this line works DbParameter param = cmd.CreateParameter(); param.ParameterName = "@ArtistID"; foreach (string artist in list) { param.Value = artist; cmd.Parameters.Add(param); using (DbDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { Console.WriteLine(artist + "\t" + reader["counts"]); //reader["counts"] is always '0' } cmd.Parameters.Clear(); } } }
}
Database table format: Artist has one value / key called ArtistID , enter int
Tracks have three columns, TrackID (int, pk) , artistid (int) , albumid(int)
Any suggestions? Thanks.
source share