Hi!
All these codes work very well. I put all here to give you a good understanding to help me.
I have a table I can save safelly without problem. But I would like to learn how can I among these code to save my current time system directly in the column of my sql table (time of my computer). In other word, if its will mess to much in my code then which value can I for the date.
private void btnSave_Facturacao_Click(object sender, EventArgs e)
{
{
conn = new SqlConnection(connstr);
comm = new SqlCommand();
conn.Open();
SqlParameter S = new SqlParameter("@S", SqlDbType.NChar);
SqlParameter data = new SqlParameter("@dataFact", SqlDbType.Date);// here
SqlParameter nomecliente = new SqlParameter("@nc", SqlDbType.NChar);
SqlParameter designacao = new SqlParameter("@desig", SqlDbType.NChar);
SqlParameter codigo = new SqlParameter("@cod", SqlDbType.VarChar);
SqlParameter quantidade = new SqlParameter("@quant", SqlDbType.NChar);
SqlParameter precocompra = new SqlParameter("@precocompra", SqlDbType.NChar);
SqlParameter precosaida = new SqlParameter("@precosaida", SqlDbType.NChar);
SqlParameter total = new SqlParameter("@total", SqlDbType.NChar);
SqlParameter lucrovenda = new SqlParameter("@lucrovenda", SqlDbType.NChar);
SqlParameter troco = new SqlParameter("@troco", SqlDbType.NChar);
SqlParameter utilizador = new SqlParameter("@utilizador", SqlDbType.NChar);
comm.Parameters.Add(S);
comm.Parameters.Add(data);//here
comm.Parameters.Add(nomecliente);
comm.Parameters.Add(designacao);
comm.Parameters.Add(codigo);
comm.Parameters.Add(quantidade);
comm.Parameters.Add(precocompra);
comm.Parameters.Add(precosaida);
comm.Parameters.Add(total);
comm.Parameters.Add(lucrovenda);
comm.Parameters.Add(troco);
comm.Parameters.Add(utilizador);
S.Value = "*";
data.Value = dateTimePicker1.Value;//here
nomecliente.Value = nomeClienteFactu.Text;
designacao.Value = artigoFactu.Text;
codigo.Value = leituraCodFactu.Text;
quantidade.Value = quantidadeFactu.Text;
precocompra.Value = precocompraFactu.Text;
precosaida.Value = precosaidaFactu.Text;
total.Value = totalFactu.Text;
lucrovenda.Value = lucrovendaFactu.Text;
troco.Value = trocos.Text;
utilizador.Value = utilizadores.Text;
//total.Value = totalFactu.Text;
//lucrovenda.Value =
//troco
//utilizador
comm.Connection = conn;
comm.CommandText = "insert into fact values(@s,@datafact,@nc,@desig,@cod,@quant,@precocompra,@precosaida,@total,@lucrovenda,@troco,@utilizador)";
try
{
comm.ExecuteNonQuery();
MessageBox.Show("saved");
nomeClienteFactu.Clear();
/*artigoFactu.Text = "";
quantidadeFactu.Clear();
precoFactu.Clear();
totalFactu.Clear();*/
}
catch (Exception)
{
MessageBox.Show("Not saved");
}
finally
{
conn.Close();
}
}
}
private void Facturacao_Load(object sender, EventArgs e)
{
}
}
}
8 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted May 6, 2014, 1:26 PM
This corresponds to SqlDbType.DateTime, not SqlDbType.Date, as it includes a time component as well.
IsraelPosted May 6, 2014, 1:37 PM
IsraelPosted May 6, 2014, 12:40 PM
VulpesPosted May 1, 2014, 1:43 PM
You can set the parameter value to any date you like and it will be inserted into the database with the values of the other columns.
If you want to read the date from a textbox, rather than a datetimepicker, in a robust way, you can do:
IsraelPosted May 1, 2014, 12:59 PM
VulpesPosted May 1, 2014, 11:58 AM
data.Value = dateTimePicker1.Value;
to this:
data.Value = DateTime.Today;
The other two lines should be OK.
IsraelPosted May 1, 2014, 11:40 AM
Nilanka DharmadasaPosted Apr 30, 2014, 7:25 PM
You can use 'Datetime' , 'time', etc.. based on your requirement. For more details check this.
http://technet.microsoft.com/en-us/library/ms186724.aspx
Convert the time to GMT before saving and then convert back to your local time zone when reading.
Or you can use 'Datetimeoffset' type to save your time. Using that type, you can save both time zone information and the time.