where should i save my drivers and advertisement files - c#

i'm creating a shopping website for buying computer parts. i want to accept advertisement(images) and also provide a way to upload drivers(exe or msi) of the items locally. i already have the following tables in the SQL Server 2005 database:
--here is the driver table
tblDriver (driver_id bigint primary key identity(1,1),
driver_name nvarchar(max) not null, driver_address nvarchar(500) not null)
--here is the ad table
tblAd (ad_id bigint primary key identity(1,1), ad_address nvarchar(500) not null)
so where should i save the actual files? i'm looking for a clean solution with no truble for CRUD operations.

As usual - you may either store the files into DB, or store them into filesystem.
Storing in the DB has its advantages - you always will have a consistent backup.
Store in filesystem for faster file read.
But you may combine both methods and store files in DB as FILESTREAM column. Although if I remember right - FILESTREAMs are available only starting from Sql Server 2008.

IMO better solution is to save the files in file system and store the path or location in database.

Related

SQL MERGE when the two tables are in different Azure databases?

I want to do what SQL MERGE...WHEN MATCHED...WHEN NOT MATCHED does,
but with the source and destination tables on different Azure databases. (Typically on the same server if that helps.)
The source and destination tables are not exactly the same, that's why SQL MERGE would be perfect. (I can decide what column to match on, and which columns to use in the INSERT or UPDATE.)
If this can not be done in SQL, I could load the tables into my C# code and do the merge there (will affect performance though). Does anyone know if .NET has something similar to SQL MERGE in ADO.NET (merging DataTables).
Thanks for any help!
Edit: this image shows the tables before MERGE (all Values are int):
This image shows the MERGE statement and the resulting source and dest:
Azure SQL database doesn't support across database operations directly, even these databases are in the same Azure SQL Server. it will throw below error.
To work around this Azure SQL database only support the across database query with elastic query
Example
CREATE MASTER KEY; -- create master key
GO
-- credential maps to a login or contained user used to connect to remote database
CREATE DATABASE SCOPED CREDENTIAL CrossDbCred1 -- credential name
WITH IDENTITY = 'username', -- login or contained user name
SECRET = '**********'; -- login or contained user password
GO
-- data source to remote Azure SQL Database server and database
CREATE EXTERNAL DATA SOURCE source
WITH
(
TYPE=RDBMS, -- data source type
LOCATION='server.database.windows.net', -- Azure SQL Database server name
DATABASE_NAME='database1', -- database name
CREDENTIAL=CrossDbCred1 -- credential used to connect to server / database
);
GO
-- external table points to table in an external database with the identical structure
CREATE EXTERNAL TABLE [dbo].[source]
(
[Id] [varchar](50),
[value1] [int],
[value2] [int],
[value3] [int]
)
WITH (DATA_SOURCE = [source], -- data source
SCHEMA_NAME = 'dbo', -- external table schema
OBJECT_NAME = 'source' -- name of table in external database
);
GO
Now we can test our Elastic Query to merge tables.
MERGE into destination1 B
USING source E
ON (B.Id = E.Id)
WHEN MATCHED THEN
UPDATE SET value2 = E.value2, value3 = E.value3
WHEN NOT MATCHED THEN
INSERT (Id,value2,value3) VALUES (Id,value2,value3);
Output
Before merging
After merging

Cannot insert the value NULL into column, table; Column does not allow nulls... But this column is Primary Key and has autoincrement property

I have published a web app, it's running on azure. The web app project I was testing on my computer (localhost) and then on the web app. When I tested to create a new row a data (for example a new person on a PersonTable) It worked on my localhost but when I tested the web app published it throws this error:
Cannot insert the value NULL into column 'ID_ANTEC_PERS', table
'Telejdb.dbo.ANTECEDENTES_PERSONALES'; column does not allow nulls.
INSERT fails. The statement has been terminated.
That column (ID_ANTEC_PERS) is the ID of that table(ANTECEDENTES_PERSONALES), but I set to this column the identity(1,1) property on my sql server DB. When I worked this project in my computer (localhost) I hadn't this problems, I think it is because the project knew the sequence of that auto increment property. But now, once the web app is published and running on azure, I wanted to see the code of my project but I realized it was compiled on .dll files So I can't edit the web app project. Any Help? Guide? Suggestions? I will appreciate that.
Thanks in advance
Check the field from your table design then Check the checkbox from your table design then it allows to insert null value in table
id is supposed to be an incrementing value.
You need to set this, or else if you have a non-nullable column, with no default value, if you provide no value it will error.
To set up auto-increment in SQL Server Management Studio:
Open your table in Design
Select your column and go to Column Properties
Under Indentity Specification, set (Is Identity)=Yes and Indentity Increment=1
in local if you are not getting this problem means you have to update the
azure database
Finally I could solve it, the problem was my azure DB tables didn't have identity property (autoincrement) so the table had a PK but it doesn't have their identity property.
What happen?
When I tried to import my local DB (which all its tables have the identity property) to azure DB I didn't by a unknown method (I used the task of SSMS: Task>Export Data...) This could import all the tables, PK Constraints and all the data but it didn't import all the identity properties.
How to fix it?
What I did to solve it was importing my database again but this time in a correct way: importing my local db to azure db by a bacpac file (azure database recognizes this kind of backup file).
Actually, before I used the method to import my DB that I used, I tried importing my db by importing bacpac file but I couldn't because I used SSMS 2014 and researching on another stackoverflow questions I figured out I can't make a bacpac file with SSMS 2014 (I had a problem with clustered Index) but I can do it with SSMS 2016, so I installed it and finally I could create the bacpac file and then I could import it to my azure db, and it had the identity property

How to store a zip file in sql server table

I have a problem and I do not know what to do. I am creating a application where the user can upload zip file. I want to store this file in my database. On Google I was not able to find any real solution for this. In my database I created a field of varchar(max) to store this. I am using C# and SQL server 2008 R2. Are there any solutions or guides that you can provide to me. >
You can not same file as nvarchar in database. Change the column datatype to varbinary.
From the code you need to convert your file to byte[] as like below.
byte[] bytes = System.IO.File.ReadAllBytes(filename);
Now save it into database as usual way.

SQL Backup to Isolated storage from ADO.NET

Is it possible to create an SQL server database backup using c# ADO.net and outputting the .BAK file to Isolated storage?
Thanks
What kind of isolated storage are you referencing?
How about:
create a stored proc to perform the backup.
BACKUP DATABASE [Foo]
TO DISK = N'\\server\directory\Foo.BAK' WITH NOFORMAT, NOINIT,
NAME = N'Foo-FullBackup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
call this stored proc on the SQL Server from your C# client code
depending on your needs (isolated storage?) use System.IO.File.Move() to move the .BAK file from its source to your destination.
No. Because there is no way to get access to the isolated storage PATH by program, you can not tell SQL Server to put things there.
Also, for anything but the most trivial applications, the size limit on isolated storage would be a joke compared what you need for a db backup.

how to do attachments for each sql record in c#

I am developing a desktop application in c# & sql 2005. With candidate data entry form I want to provide option to attach required documents(in pdf format) wid data. Kindly let me know the best method. Thank you in advance.
Simply create a table that will contain the filename and server path for the file to be attached, then create a method to copy the attached file to the server location and store the relivant information (name and path) in the table. Use other methods to retrive the file from the server location when requested. Simple.
I personaly prefer to store the documents as BLOBs since server file structures and paths can change over time.
Well then unfortunately you have to either manage the file storage yourself using the servers file system, or you could store it in the db itself (IT WILL GET BLOATED!!!)
See sql server 2005 file storage
How To: Encrypt and Manage Documents with SQL Server 2005
OK, then for file management
see this example
File Manager Component

Categories

Resources