I have three columns
Fraquence int
[Prochain étalonnag] date
[date étalonnage] type:date
In Visual Studio, I have a form for adding a new (etalonnage) to my table the user inputs two vules ( Fraquence
and date étalonnage
).
I want to do this with a stored procedure in SQL Server :
Prochain étalonnag = date étalonnage + (Month Fraquence).
The column Prochain étalonnage
will be filled automatically when the user click in button Add
.
Please check following .NET code, sorry it is in VB.NET But it is not a problem to convert it to C#
The stored procedure in database is named prAddRow for this sample It accepts two parameters @p_int and @p_date
Dim sqlConnBuilder As New SqlConnectionStringBuilder()
sqlConnBuilder.DataSource = txtSQLServer.Text
sqlConnBuilder.InitialCatalog = txtDatabase.Text
sqlConnBuilder.IntegratedSecurity = True
conn = New SqlConnection(sqlConnBuilder.ToString)
Dim cmd As New SqlCommand("prAddRow")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@p_int", SqlDbType.Int).Value = 1
cmd.Parameters.Add("@p_date", SqlDbType.Date).Value = Date.Now.Date
conn.Open()
cmd.Connection = conn
Dim numberOfAffectedRows As Integer = cmd.ExecuteNonQuery()
conn.Close()
The T-SQL source codes for the sample stored procedure is as follows
create procedure prAddRow(
@p_int int,
@p_date date
)
as
insert into etalonnage (Fraquence , [date etalonnage] ) values (@p_int, @p_date)
go
Note: I renamed stored procedure according to marc's note from sp_addRow to prAddRow
I hope it is useful for you
private void button1_Click(object sender, EventArgs e)
{
cmd = new SqlCommand("INSERTNEW", connection);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter [] par = new SqlParameter[4];
par[0] = new SqlParameter("@COD", SqlDbType.VarChar, 50);
par[0].Value = textBox1.Text;
par[1] = new SqlParameter("@FERQ", SqlDbType.Int);
par[1].Value = numericUpDown1.TextAlign;
par[2] = new SqlParameter("@DATE_ET", SqlDbType.Date);
par[2].Value = dateTimePicker1.Text;
par[3] = new SqlParameter("@DATE_PROC", SqlDbType.Date);
DateTime d1 = Convert.ToDateTime(dateTimePicker1.Text);
int k =Convert.ToInt32(numericUpDown1.TextAlign);
string r = d1.AddMonths(k).ToShortDateString();
par[3].Value = r ;
cmd.Parameters.AddRange(par);
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
MessageBox.Show("good add");
}
this is work thanks all
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.