private static void insertTest(testItem test) { SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString); string str = "insert into test (name, user_id, cat_id) values (@name, @user_id, @cat_id)"; SqlCommand cmd = new SqlCommand(str, con); try { con.Open(); cmd.Parameters.AddWithValue("name", test.Name); cmd.Parameters.AddWithValue("user_id", test.UserId); cmd.Parameters.AddWithValue("cat_id", test.CategoriesID); cmd.ExecuteNonQuery(); } catch (Exception) { throw new ApplicationException("Не удается добавить тест"); } finally { con.Close(); } }
private static testItem SelectTempTest(string id) { SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString); string str = "select * from temptest where test_id = @id"; SqlCommand cmd = new SqlCommand(str, con); cmd.Parameters.AddWithValue("id", id); try { con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); string [] test= new string[dr.FieldCount]; testItem item = new testItem(dr["test_id"].ToString(), dr["name"].ToString(), dr["user_id"].ToString(), dr["cat_id"].ToString()); return item; } } catch (Exception) { throw new ApplicationException("Тест не найден"); } finally { con.Close(); } return null; }