//поиск по id public bool FindStorage(string stringConnection, int _Id) { Logger logger = LogManager.GetCurrentClassLogger(); logger.Info("Inside"); SqlConnection conn = new SqlConnection(stringConnection); conn.Open(); StorageTest ob = new StorageTest(); string str = @"select id from Test where id = @_Id"; SqlCommand cmd = new SqlCommand(str, conn); cmd.Parameters.AddWithValue("@_Id", _Id); SqlDataReader rdb = cmd.ExecuteReader(); rdb.Read(); if (conn != null) { conn.Close(); conn.Dispose(); } else { logger.Warn("SQlConnection not close"); } if (rdb == null) { rdb.Dispose(); rdb.Close(); logger.Info("Not find element"); return false; } logger.Info("Find element"); rdb.Close(); rdb.Dispose(); return true; }
//получение класс StorageTest по id public StorageTest GetSQlData(string stringConnection, int _Id) { Logger logger = LogManager.GetCurrentClassLogger(); logger.Info("Inside"); SqlConnection conn = new SqlConnection(stringConnection); conn.Open(); StorageTest test = new StorageTest(); string sqlGetCmd = "select first,second from Test where Id = @_Id"; SqlCommand cmd = new SqlCommand(sqlGetCmd, conn); cmd.Parameters.AddWithValue("@_Id", _Id); SqlDataReader rdb = cmd.ExecuteReader(); rdb.Read(); //if (rdb.FieldCount!=2) //{ // logger.Warn("Count elemen != 2"); // rdb.Close(); // return null; //} test.IdTest = _Id; if (rdb.IsDBNull(rdb.GetOrdinal("first"))) { test.First = null; } else { test.First = rdb.GetString(rdb.GetOrdinal("first")); } if (rdb.IsDBNull(rdb.GetOrdinal("second"))) { test.Second = rdb.GetInt32(rdb.GetOrdinal("second")); } else { test.Second = -1; } if (conn != null) { conn.Close(); conn.Dispose(); } if (rdb != null) { rdb.Close(); conn.Dispose(); logger.Info("Method close"); return test; } else { logger.Warn("Not return element"); return null; } }
//params object[] values //добавление public void SetSQlData(string stringConnection, StorageTest test) { Logger logger = LogManager.GetCurrentClassLogger(); logger.Info("Inside"); SqlConnection conn = new SqlConnection(stringConnection); conn.Open(); string strInsert = @"insert into Test(first,second) values (@_First,@_Second)"; SqlCommand cmd = new SqlCommand(strInsert, conn); cmd.Parameters.AddWithValue("@_First", test.First); cmd.Parameters.AddWithValue("@_Second", test.Second); cmd.ExecuteNonQuery(); if (conn != null) { conn.Close(); conn.Dispose(); } else { logger.Warn("SQlConnection not close"); } }
public void Post(int id) { StorageTest temp = new StorageTest {IdTest=1,First="begin",Second=7 }; reposTest.SetSQlData(stringConnection, temp); }