First of all, never concatenate SQL strings like that! Use @param place holders and SqlCommand.Parameters collection. For example:
SqlCommand cmd = new SqlCommand("SELECT * FROM MyTable WHERE Foo = @foo");
cmd.Parameters.AddWithValue("foo", some_value);
Second, I'd suggest you to make stored procedure which handles inserting. Something like:
Code:
CREATE PROCEDURE InsertTiming(@timing nvarchar(255))
BEGIN
SET NOCOUNT ON;
-- Check if timing already exists
DECLARE @tmp nvarchar(255);
SELECT @tmp = s_time FROM timing WHERE s_time = @timing;
IF @tmp IS NOT NULL
BEGIN
raiserror(N'Timing %s already assigned',
10,
1,
@timing);
END
-- Insert new value
INSERT INTO timing(s_test) VALUES (@timing);
END
Then just call it like this:
Code:
SqlCommand cmd = new SqlCommand("InsertTiming", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("timing", test);
try
{
cmd.ExecuteNonQuery();
}
catch (SqlException e)
{
MessageBox.Show(e.Message);
}