public QrCodeEntity Read(int id) { QrCodeEntity qrCode = new QrCodeEntity(); 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 QrCodeEntity Read(int id) { QrCodeEntity entity = new QrCodeEntity(); string sql = $"SELECT * FROM {GetTableName()} WHERE ID= @id"; using (SqlConnection sqlConnection = new SqlConnection(GetConnectionString())) { sqlConnection.Open(); SqlParameter IdParam = new SqlParameter("@id", id); using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection)) { sqlCommand.Parameters.Add(IdParam); SqlDataReader reader = sqlCommand.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { entity.Id = Int32.Parse(reader["Id"].ToString()); entity.Content = (byte[])reader["Content"]; entity.QrCodeType = (QrCodeType)reader["QrCodeType"]; } } else { throw new Exception("No data found!"); } } return(entity); } }
static void Main(string[] args) { #region AddProduct /* Console.WriteLine(); * decimal cost = 1000; * string name = "Vaccum Cleaner"; * * string sqlTemplate = "insert into [dbo].[products]([ProductName],[Cost]) " + * "values(@productName, @cost)"; * * ConnectionStringInAppConfigDemo appConfig = * new ConnectionStringInAppConfigDemo(); * * using (SqlConnection sqlConnection = new SqlConnection(appConfig.GetConnectionString())) * { * sqlConnection.Open(); * using (SqlCommand sqlCommand = new SqlCommand(sqlTemplate, sqlConnection)) * { * SqlParameter parameterI = new SqlParameter("@productName", name); * SqlParameter parameterII = new SqlParameter("@cost", cost); * * sqlCommand.Parameters.Add(parameterI); * sqlCommand.Parameters.Add(parameterII); * * sqlCommand.ExecuteNonQuery(); * } * }*/ #endregion //QrCodeGeneratorService qrCodeGeneratorService = new QrCodeGeneratorService(); //qrCodeGeneratorService.GetQrCodePurchaseInfo(1, 1); QrCodeRepository qrRep = new QrCodeRepository(); QrCodeEntity entity = qrRep.Read(1); //QRCodeData data = new QRCodeData(entity.Content, QRCodeData.Compression.Uncompressed); //QRCode qRCode = new QRCode(/*data*/); using (MemoryStream ms = new MemoryStream()) { ms.Write(entity.Content, 0, entity.Content.Length); Bitmap qrCodeImage = new Bitmap(ms); qrCodeImage.Save(@"C:\Users\Zhanibek\Desktop\Bitqr-codes\111.png"); } /*TransactionDemo tDemo = new TransactionDemo(); * tDemo.MakeOrderProcess(1, "Computer", 1);*/ // Console.WriteLine(sqlTemplate); Console.ReadLine(); }
public void Update(int id, QrCodeEntity 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(QrCodeEntity 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(); } } }
public void Add(QrCodeEntity entity) { 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 userIdParam = new SqlParameter("@userId", entity.UserId); SqlParameter contentParam = new SqlParameter("@content", entity.Content); SqlParameter qrCodeTypeParam = new SqlParameter("@qrCodeType", (int)entity.QrCodeType); command.Parameters.Add(userIdParam); command.Parameters.Add(contentParam); command.Parameters.Add(qrCodeTypeParam); command.ExecuteNonQuery(); } } }