What is the correct way to do this?
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//Console.WriteLine(reader["name"].ToString());
SendFax(reader["title"].ToString(),
reader["filepath"].ToString(),
reader["name"].ToString(),
reader["name"].ToString());
}
Also, how do you check to see if it returns any rows in an if statement?
Like:
if($numrows>"0")
{
//execute code
}
else
{
//do nothing
}
Full Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using FAXCOMLib;
namespace MysqlConnection1
{
class Program
{
static void Main(string[] args)
{
string connString = "Server=localhost;Port=3306;Database=test;Uid=myuser;password=mypassword;";
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
command.CommandText = "SELECT * FROM firstcsharp";
//command.CommandText = "UPDATE blah blah";
//conn.Open();
//conn.ExecuteNonQuery();
//conn.Close();
try
{
conn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
MySqlDataReader reader = command.ExecuteReader();
if(reader.HasRows){
while (reader.Read())
{
//Console.WriteLine(reader["name"].ToString());
SendFax(reader["title"].ToString(),reader["filepath"].ToString(),reader["name"].ToString(),reader["name"].ToString());
}
}
//Console.ReadLine();
public void SendFax(string DocumentName, string FileName, string RecipientName, string FaxNumber)
{
if (FaxNumber != "")
{
try
{
FAXCOMLib.FaxServer faxServer = new FAXCOMLib.FaxServerClass();
faxServer.Connect(Environment.MachineName);
FAXCOMLib.FaxDoc faxDoc = (FAXCOMLib.FaxDoc)faxServer.CreateDocument(FileName);
faxDoc.RecipientName = RecipientName;
faxDoc.FaxNumber = FaxNumber;
faxDoc.DisplayName = DocumentName;
int Response = faxDoc.Send();
faxServer.Disconnect();
}
catch(Exception Ex){MessageBox.Show(Ex.Message);}
}
}
}
}
Errors:
Error 1 } expected c:\documents and settings\bruser\my documents\visual studio 2010\Projects\FirstMysqlConnection\MysqlConnection1\Program.cs 41 14 MysqlConnection1
Error 2 An object reference is required for the non-static field, method, or property ‘MysqlConnection1.Program.SendFax(string, string, string, string)’ c:\documents and settings\bruser\my documents\visual studio 2010\Projects\FirstMysqlConnection\MysqlConnection1\Program.cs 38 17 MysqlConnection1
Error 3 Interop type ‘FAXCOMLib.FaxServerClass’ cannot be embedded. Use the applicable interface instead. c:\documents and settings\bruser\my documents\visual studio 2010\Projects\FirstMysqlConnection\MysqlConnection1\Program.cs 50 52 MysqlConnection1
Error 4 The name ‘MessageBox’ does not exist in the current context c:\documents and settings\bruser\my documents\visual studio 2010\Projects\FirstMysqlConnection\MysqlConnection1\Program.cs 68 25 MysqlConnection1
Edit: Regarding your error messages:
Mainmethod (add another}at the end). One way to help with this is to keep matching braces on the same vertical line so they are easy to see – tab them over to match. Also, you can use Edit -> Advanced -> Format Document feature in Visual Studio to help see the mismatch.SendFax, which is non-static, fromMain, which is static. This is easily fixed by addingstaticto make itpublic *static* void SendFax.MessageBoxclass you need to add a reference toSystem.Windows.Forms.dlllibrary and also to the namespaceSystem.Windows.Formsat the top.The code you have now looks like it should work. One thing to note is that if
SendFaxis a long running operation, you will probably want to either run it asynchronously, or download all the data from the database at once and process it afterward so that the connection can be closed. Connections to databases should be opened for as short as possible.Regarding your second question, using the above method it’s as simple as checking
Otherwise, you can do something like this: