Forcing code-first to always initialize a non-existent database?

Initializer is executed when you need to access the database so if you want to create database on app start use anything of the following: context.Database.Initialize(true); //If set to true the initializer is run even if it has already been run. context.Database.Create() http://msdn.microsoft.com/en-us/library/system.data.entity.database.initialize(v=vs.103).aspx CreateDatabaseIfNotExists An implementation of IDatabaseInitializer that will recreate and optionally re-seed the … Read more

How do you Sort a DataTable given column and direction?

I assume “direction” is “ASC” or “DESC” and dt contains a column named “colName” public static DataTable resort(DataTable dt, string colName, string direction) { DataTable dtOut = null; dt.DefaultView.Sort = colName + ” ” + direction; dtOut = dt.DefaultView.ToTable(); return dtOut; } OR without creating dtOut public static DataTable resort(DataTable dt, string colName, string direction) … Read more

Do we have transactions in MS-Access?

Nobody has actually given you any code examples here in the answer or even cited an example (the Access help files do include examples, though). The key issue to keep in mind is that in Jet/ACE (Access does not support transactions itself — it depends on whatever database engine you’re using for that) that the … Read more

How to serialize a large graph of .NET objects into a SQL Server BLOB without creating a large buffer?

There is no built-in ADO.Net functionality to handle this really gracefully for large data. The problem is two fold: there is no API to ‘write’ into a SQL command(s) or parameters as into a stream. The parameter types that accept a stream (like FileStream) accept the stream to READ from it, which does not agree … Read more

How to give ADO.NET Parameters

Should use something like the following: SqlCommand cmd = new SqlCommand(“INSERT INTO Product_table Values(@Product_Name, @Product_Price, @Product_Profit, @p)”, connect); cmd.Parameters.Add(“@Product_Name”, SqlDbType.NVarChar, ProductNameSizeHere).Value = txtProductName.Text; cmd.Parameters.Add(“@Product_Price”, SqlDbType.Int).Value = txtProductPrice.Text; cmd.Parameters.Add(“@Product_Profit”, SqlDbType.Int).Value = txtProductProfit.Text; cmd.Parameters.Add(“@p”, SqlDbType.NVarChar, PSizeHere).Value = txtP.Text; cmd.ExecuteNonQuery(); Assuming @p parameter is some NVarChar. Better avoid using AddWithValue, see why here: https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ Also at INSERT SQL … Read more

How can I Pass a Table Name to SqlCommand?

SqlCommand.Parameters are supported for Data manipulation language operations not Data definition language operations. Even if you use DML, you can’t parameterize your table names or column names etc.. You can parameterize only your values. Data manipulation language = SELECT … FROM … WHERE … INSERT INTO … VALUES … UPDATE … SET … WHERE … … Read more

how do I mock sqlconnection or should I refactor the code?

SqlBulkWriter class is tightly coupled to implementation concerns which make it difficult to test the class in isolation. Knowing the connection string is not really a concern of that class and can be delegated out to another service. Something like public interface IDbConnectionFactory { IDbConnection CreateConnection(); } and its implementation for your class would look … Read more