MsSQL and C# in VisualStudio
-
I needed a local database. Maybe I misunderstood something and chose it. MsSql local♪ Anyway, I need a local data repository to be transferred with the project. I think I chose correctly.
I created the architecture of the future base. The whole project is divided into assembly. Created. DAL Library in one decision Out.♪ Then I decided to test the base and architecture. Created another library console. Application and started writing tests. I had to write first. generator And when she was done, I got some problems. I found out it's starting.
ConnectionStrings
Only from the test project, and I thought it was very strange.Let's go.
ConnectionStrings
DAL I don't understand. Coneks In both cases, it is the same, but it works according to different contexts.Here's the tip:
<connectionStrings> <clear/> <add name="DalClickBot.Properties.Settings.ClickBotDataBaseConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ClickBotDataBase.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
The anomaly is as follows. The database file is copied to the test assembly and the entire databox is maintained in this collection ♪♪ ♪ Total DAL is not touched. GS♪
The database file itself is not in the project, not in the project. debaga♪ I only know that he exists. SQL Server Object Explorer♪ No sign of servers, no place else. GS♪ I got the records on the database. Dal in such a way:
using (SqlConnection conn = new SqlConnection(@"Data Source= (LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ClickBotDataBase.mdf; Integrated Security=True")) { // conn.ConnectionString = ConfigurationManager.ConnectionStrings["DalClickBot.Properties.Settings.Click BotDataBaseConnectionString"].ConnectionString; //Console.WriteLine(conn.ConnectionString); conn.Open(); SqlCommand comm = new SqlCommand(sql, conn); comm.ExecuteNonQuery(); }
To be honest, he's not very comfortable with me, I wish I could. configuration♪ When I do that, I'm going to catch this mistake:
Серьезность Код Описание Проект Файл Строка Предупреждение Не удалось скопировать "C******\DalClickBot \ClickBotDataBase_log.ldf" в "bin\Debug\ClickBotDataBase_log.ldf". Начинается повторная попытка 1 в 1000 мс. Процесс не может получить доступ к файлу "bin\Debug\ClickBotDataBase_log.ldf", так как этот файл используется другим процессом. TestDalClickBot
These are:
- why there's a copying if it's right. GS?
- If it's not right, how do you fix it?
- I don't know.
ConnectionStrings
of Dal assembly?
-
- You're doing the right thing. I just need to understand that your DAL is just a library. The annex is started in the context of your test project and construction, taking its profile. That's why you should duplicate the connection string originally announced in the DAL.
For correct relative pathways, the DataDirectory parameter has been introduced in connection string. Like ASP. NET projects he points to the director of App_Data. You can ask DataDirectory software when the application starts. That is, to determine the path to which your OBD will rest and to establish it in the settings.
var path = ... AppDomain.CurrentDomain.SetData("DataDirectory", path);
Either you can make an absolute way to mdf the file. In both cases, your connection string will be correct. More detailed about DataDirectory looking for MSDN.
- It's a declarative thing. The DAL method can be programmed to read connection string from the DAL configuration. And the result is used to connect with the OBD.
The situation is normal. Your DAL is a reused library. It should not depend on the details of the environment. I'm telling you, where she said she was writing.