Hi guys currently i have this code. I would like to add another field " GivenName"
into the statement but the issue is it is from another table. How do i merge two database tables into this statement without altering the original query output.
namespace Test
{
public partial class frmSales : Form
{
public frmSales()
{
InitializeComponent();
}
private void dtpFrom_ValueChanged(object sender, EventArgs e)
{
}
private void btnExtract_Click(object sender, EventArgs e)
{
SqlConnection objConn = new SqlConnection("Data Source=abcdefg;Initial Catalog=hello;Persist Security Info=True;User ID=sa;Password=");
objConn.Open();
SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), InvDate,3) AS InvDate,InvoiceNo,EmployerCode,TaxAmount + SubTotal AS Amount,'' AS Payment FROM Invoice WHERE (InvDate >= CONVERT(datetime, '" + dtpFrom.Text + "', 105 )) AND (InvDate <= CONVERT(datetime, '" + dtpTo.Text + "', 105))", objConn);
SqlDataReader objReader;
objReader = objCmd.ExecuteReader();
System.IO.FileStream fs = new System.IO.FileStream("C:\\CMSExportedData\\Sales-" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", System.IO.FileMode.Create);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);
int count = 0;
while (objReader.Read())
{
for (int i = 0; i < 5; i++)
{
if (!objReader.IsDBNull(i))
{
string s;
s = objReader.GetDataTypeName(i);
//MessageBox.Show(s);
if (objReader.GetDataTypeName(i) == "char")
{
sw.Write(objReader.GetString(i));
}
else if (objReader.GetDataTypeName(i) == "money")
{
sw.Write(objReader.GetSqlMoney(i).ToString());
}
else if (objReader.GetDataTypeName(i) == "nvarchar")
{
sw.Write(objReader.GetString(i));
}
}
if (i < 4)
{
sw.Write("\t");
}
}
count = count + 1;
sw.WriteLine();
}
sw.Flush();
fs.Close();
objReader.Close();
objConn.Close();
MessageBox.Show(count + " records exported successfully.");
this.Close();
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void dtpTo_ValueChanged(object sender, EventArgs e)
{
}
private void frmSales_Load(object sender, EventArgs e)
{
}
}
}
Change the SQL statement, so that you JOIN the 2 tables together. Then, you can access the other column.
Use a JOIN
:
SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), InvDate,3) AS InvDate,InvoiceNo,EmployerCode,TaxAmount + SubTotal AS Amount,'' AS Payment
FROM Invoice inv INNER JOIN
[YourOtherTable] tab ON inv.[SomeID] = tab.[SomeID]
WHERE (InvDate >= CONVERT(datetime, '" + dtpFrom.Text + "', 105 ))
AND (InvDate <= CONVERT(datetime, '" + dtpTo.Text + "', 105))", objConn);
INNER JOIN
if it's a NOT NULL
column and LEFT JOIN
when the column is NULL
.
So you will add this:
FROM Invoice inv INNER JOIN
[YourOtherTable] tab ON inv.[SomeID] = tab.[SomeID]
EDIT:
Okay, it seems that [MedicalRecordID] is the most obvious.
FROM [Invoice] inv INNER JOIN
[PatientDetails] tab ON inv.[MedicalRecordID] = tab.[MedicalRecordID]
Just remember, if you are looking for a specific PatientDetails
record you will need to add another AND
to your WHERE
clause.
Something like:
AND tab.[LastName] = 'SomeLastName'
EDIT:
SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), inv.[InvDate],3) AS InvDate
, inv.[InvoiceNo]
, inv.[TaxAmount] + inv.[SubTotal] AS Amount
, '' AS Payment
FROM [Invoice] inv LEFT JOIN
[PatientDetails] tab ON inv.[MedicalRecordID] = tab.[MedicalRecordID]
WHERE (inv.[InvDate] >= CONVERT(datetime, '" + dtpFrom.Text + "', 105 ))
AND (inv.[InvDate] <= CONVERT(datetime, '" + dtpTo.Text + "', 105))", objConn);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.