Here's how you create a CSV file that will open in Excel. It is a csv (Comma spaced Value) file with double quotes " as identifiers.
Notice that fileStream.setLength(0) makes it so you will overwrite whatever data is in the file you are saving to.
private void ButtonRunCSV_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
String saveFileName = saveFileDialog1.FileName;
FileStream fileStream = new FileStream(saveFileName, System.IO.FileMode.OpenOrCreate);
fileStream.SetLength(0);
StreamWriter sw = new StreamWriter(fileStream);
sw.WriteLine("Company Name,"
+ "Phone Number,"
+ "Call Date,"
+ "Call Length,"
+ "First Name,"
+ "Last Name,"
+ "Contact Title,"
+ "Gender,"
+ "Address,"
+ "City,"
+ "State,"
+ "Zip,"
+ "Country,"
+ "Email,"
+ "Fax,"
+ "Second Phone Number,"
+ "Website,"
+ "Google Verified,"
+ "Yahoo Verified,"
+ "Indexed Pages Online,"
+ "Site Links,"
+ "Site References,"
+ "Google Reviews,"
+ "Yahoo Reviews,"
+ "Web Reviews,"
+ "Annual Sales,"
+ "Employees,"
+ "SIC Code,"
+ "Industry,"
+ "Company Description,"
+ "Latitude,"
+ "Longitude");
foreach (Lead l in leads)
{
lead = l;
findLeadInfo();
if (!lead.IsClient)
{
string newLine = "";
newLine += (!String.IsNullOrEmpty(lead.CompanyName)) ? ToCSVDelimitedString(lead.CompanyName) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.PhoneNumber)) ? ToCSVDelimitedString(lead.PhoneNumber) + "," : ",";
newLine += (lead.CallDate != DateTime.MinValue) ? ToCSVDelimitedString(lead.CallDate.ToString("MMM d, yyyy h:mm tttt") + " PST") + "," : ",";
newLine += (lead.SecondsOnPhone != -1) ? ToCSVDelimitedString(TimeSpan.FromSeconds(lead.SecondsOnPhone).Minutes.ToString() + " Min") + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.ContactFirstName)) ? ToCSVDelimitedString(lead.ContactFirstName) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.ContactLastName)) ? ToCSVDelimitedString(lead.ContactLastName) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.ContactTitle)) ? ToCSVDelimitedString(lead.ContactTitle) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.ContactGender)) ? ToCSVDelimitedString(lead.ContactGender) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.Address)) ? ToCSVDelimitedString(lead.Address) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.City)) ? ToCSVDelimitedString(lead.City) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.State)) ? ToCSVDelimitedString(lead.State) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.ZipCode)) ? ToCSVDelimitedString(lead.ZipCode) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.Country)) ? ToCSVDelimitedString(lead.Country) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.Email)) ? ToCSVDelimitedString(lead.Email) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.FaxNumber)) ? ToCSVDelimitedString(lead.FaxNumber) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.PhoneNumber2)) ? ToCSVDelimitedString(lead.PhoneNumber2) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.Website)) ? ToCSVDelimitedString(lead.Website) + "," : ",";
newLine += ((lead.GoogleVerified)) ? "Yes," : ",";
newLine += ((lead.YahooVerified)) ? "Yes," : ",";
newLine += (!String.IsNullOrEmpty(lead.IndexedPages)) ? ToCSVDelimitedString(GetTheNumberWithinTheTag(lead.IndexedPages)) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.SiteLinks)) ? ToCSVDelimitedString(GetTheNumberWithinTheTag(lead.SiteLinks)) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.SiteReferences)) ? ToCSVDelimitedString(GetTheNumberWithinTheTag(lead.SiteReferences)) + "," : ",";
newLine += (lead.GoogleReviewsCount > 0) ? ToCSVDelimitedString(lead.GoogleReviewsCount.ToString()) + "," : ",";
newLine += (lead.YahooReviewsCount > 0) ? ToCSVDelimitedString(lead.YahooReviewsCount.ToString()) + "," : ",";
newLine += (lead.WebReviewsCount > 0) ? ToCSVDelimitedString(lead.WebReviewsCount.ToString()) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.AnnualSales)) ? ToCSVDelimitedString(lead.AnnualSales) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.Employees)) ? ToCSVDelimitedString(lead.Employees) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.SICCode)) ? ToCSVDelimitedString(lead.SICCode) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.Industry)) ? ToCSVDelimitedString(lead.Industry) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.Description)) ? ToCSVDelimitedString(lead.Description) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.Latitude)) ? ToCSVDelimitedString(lead.Latitude) + "," : ",";
newLine += (!String.IsNullOrEmpty(lead.Longitude)) ? ToCSVDelimitedString(lead.Longitude) + "," : "";
sw.WriteLine(newLine);
}
}
sw.Close();
}
}
private String ToCSVDelimitedString(String field)
{
return "\"" + field.Replace("\"", "\"\"") + "\"";
}
private String GetTheNumberWithinTheTag(String tag)
{
int last = 0;
last = tag.IndexOf('<', 1);
if (last > 0)
return tag.Substring(tag.IndexOf('>') + 1, last - tag.IndexOf('>') - 1);
else
return tag.Substring(tag.IndexOf('>') + 1, tag.Length - tag.IndexOf('>') - 1);
}
Popular Posts
-
Adam, Adam, thou art perfect before me. Thou shalt not lose thy children nor shalt thou see the bars of prison, jail, or cell ever again. ...
-
You can use an XML string to pass any number of options to a SQL stored procedure like so Use a scalar valued function to get one value a...
-
Here is how to parse a date that is in the format of mm/dd/yyyy: // Parse string with date but no time component. string dateStr...
-
Last night I thought about how stable God is. He plants his promises in our hearts and watches them grow. As men, we foolishly think that ...
-
Tonight I attended a conference hosted by The Church of Jesus Christ of Latter-day Saints. It's topic was the protection of the institu...
-
Agency is the most beautiful part of the gospel of Jesus Christ. Because of it, as Joseph Smith said we are free. Parents of children want...
-
View the complete solution at https://github.com/adamvaul/React-Server-Side-Web-Forms . I love ReactJS.Net. It is a framework for manag...
Thursday, February 3, 2011
Wednesday, February 2, 2011
Connecting Two Remote Databases in a single SQL Query

Background
Every Microsoft SQL Server database, there is a folder called Server Objects. Within that folder there is a folder called "Linked Servers". If you right click on that folder and hit New Linked Server, it will open up a new linked server wizard.
New Linked Server Wizard
By following the steps there, you can setup a new linked server.
First, type in the name or IP address of the server where it says "Linked server". For example: "1.0.9.46".
For a remote SQL database choose SQL Server as the server type. Then select the security page from the left, and enter the login credentials under the "Be made using this security context" option: Enter the username where it says remote login, and the password below where it says "With password."
Creating a Query using the Linked server.
Once your linked server is setup, you can execute a query like the following:
SELECT *
FROM
[1.0.9.46].DatabaseName.dbo.tableName t
JOIN users ON user.ID = t.userID;
Enjoy!!!
Subscribe to:
Comments (Atom)