public QrCodeGeoEntity Read(int id) { QrCodeGeoEntity qrCode = new QrCodeGeoEntity(); string sql = $"Select * from {GetTableName()} where id={id}"; using (SqlConnection sqlConnection = new SqlConnection(GetConnectionString())) { sqlConnection.Open(); using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection)) { SqlDataReader reader = sqlCommand.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { qrCode.Id = Int32.Parse(reader["Id"].ToString()); qrCode.UserId = Int32.Parse(reader["UserId"].ToString()); qrCode.QrCodeType = (QrCodeType)reader["QrCodeType"]; qrCode.Content = (byte[])reader["Content"]; } } else { throw new Exception("No data found!"); } } } return(qrCode); }
public void Update(int id, QrCodeGeoEntity updated) { string sqlCommand = $"Update {GetTableName()} set UserId=@userId, QrCodeType=@qrCodeType, Content=@content where Id=@id"; using (SqlConnection sqlConnection = new SqlConnection(GetConnectionString())) { sqlConnection.Open(); using (SqlCommand command = new SqlCommand(sqlCommand, sqlConnection)) { SqlParameter userId = new SqlParameter("@userId", updated.UserId); SqlParameter qrCodeType = new SqlParameter("@qrCodeType", (int)updated.QrCodeType); SqlParameter content = new SqlParameter("@content", updated.Content); command.Parameters.Add(userId); command.Parameters.Add(qrCodeType); command.Parameters.Add(content); command.ExecuteNonQuery(); } } }
public void Insert(QrCodeGeoEntity qrCode) { string sqlCommand = $"Insert into {GetTableName()} (UserId, Content, QrCodeType) " + $"Values (@userId, @content, @qrCodeType)"; using (SqlConnection sqlConnection = new SqlConnection(GetConnectionString())) { sqlConnection.Open(); using (SqlCommand command = new SqlCommand(sqlCommand, sqlConnection)) { SqlParameter userId = new SqlParameter("@userId", qrCode.UserId); SqlParameter content = new SqlParameter("@content", qrCode.Content); SqlParameter qrCodeType = new SqlParameter("@qrCodeType", (int)qrCode.QrCodeType); command.Parameters.Add(userId); command.Parameters.Add(content); command.Parameters.Add(qrCodeType); command.ExecuteNonQuery(); } } }