How to debug database project? - c#

Ok, I'm using Quartz.NET, I have a table, when the database in that table change (after inserted, updated) I have to download a file from url, then parse that file, and insert data to another table....
I do all of this in SQL CLR Trigger. Because I dont know when the data is changed. And it is changed by other application/web service, not the current application.
So, again, I have an SQL CLR Trigger and I want to debug it when it's deploying.
I found an article here, but noluck. I cannot find a way to create the test script in step 3
http://msdn.microsoft.com/en-us/library/ms165052(v=vs.100).aspx
It seems that my Visual Studio 2013 has no option for "Set as Default Debug Script." or did I miss something?
Anybody here have debugged with Database Project?
Please walk me though this, or help me with any suggestion.
SQL CLR Trigger
public class Triggers
{
[SqlTrigger(Name = "AfterMarketSessionInserted", Target = "MarketSession",
Event = "FOR INSERT")]
public static void AfterMarketSessionInserted()
{
using (var connection = new SqlConnection("context connection=true"))
{
connection.Open();
var command = new SqlCommand("SELECT * FROM INSERTED", connection);
var reader = command.ExecuteReader();
// ... SOME CODE THAT NEED TO BE DEBUGGED.
}
}
}

Related

C# + SQL + Xamarin Android:

I've been stumped on this problem for a while, and although I have searched up my problems online, I haven't had much luck. So in my Android program made in Xamarin, in the button click event, I try to send data to the server, however, I would get this error:
System.InvalidOperationException: Update requires the command clone to have a connection object. The Connection property of the command clone has not been initialized.
I am using this code at this point:
using (SqlConnection connection = new SqlConnection(cs))
{
connection.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter($"Select * from Room", connection);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
Spinner roomsSPNR = FindViewById<Spinner>(Resource.Id.rooms);
int room = Convert.ToInt32(roomsSPNR.SelectedItem.ToString());
DataRow[] selected = rooms.Tables[0].Select($"RoomNo = {room}");
selected[0][3] = Id;
dataAdapter.Update(rooms); //Line giving the error
}
changeColors(Id);
I'm stumped on this, I can't really figure out why it is doing this. Can anyone help?
I have tried:
Setting the update.connection manually
Making an SQL command and putting the command builder's command into there(There is another error)
Setting the DataAdapter's update command to the method for the command builder
In the DataAdapter's initialization, I have changed the connection to the connection string
Many others that may not have any importance
Edit: Earlier in the code the program is able to read from the server and put it into 'rooms. On the other hand, I'm not familiar with making web services; is that what I need to do to allow it to update the database?
You cannot directly connect with a database in xamarin. You need to first create a webservice and use that web service to insert or fetch data from database.
If you need I will help you with webservices.

C# Data Access Tracing for ADO.Net - Need to do profiler to check what are all connection is being shared across multiple threads

I have tried to enable Data Access Tracing using ETW as explained in this article.
To verify that it traces the event, I have created a simple console application instead of the MVC application as it is there in the article. Code as follows, it is intended to throw so i can verify the trace works.
var connectionString = "Data Source=local;Trusted_Connection = True; Initial Catalog = Data; Timeout = 2; Max Pool Size = 4";
using (var conn = new SqlConnection(connectionString))
{
using (var command = new SqlCommand("SELECT * FROM DATA", conn))
{
conn.Open();
var reader = command.ExecuteReader();
using (var data = new SqlCommand("SELECT * FROM PEOPLE", conn))
{
var reader1 = command.ExecuteReader();
}
}
}
I am using .Net Framework v4.0.30319.
Ideally i want to trace the connection objects which are being shared between threads. Any help would be much appreciated!
Thanks
TL;DR Try registering the other .dll/mof pairs instead of the one recommended by your guide.
The steps to manually register the DLL (modified from this MS guide)
Add the following registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\BidInterface\Loader
Create a new string value (note the ':' in the key) and Modify... it
Value Name = ":Path"
Value Data = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\AdoNetDiag.dll"
Register ETW Providers in your WMI repository with mofcomp.exe (installed with windows)
mofcomp.exe C:\Windows\Microsoft.NET\Framework64\v4.0.30319\adonetdiag.mof
To test:
logman start test -p "System.Data.1" -o test.etl -ets // Start a trace
// Do things with ADO.NET (run your sample)
logman stop test -ets // Stop a started trace
tracerpt.exe test.etl -of CSV -o dump.csv // make it human readable
If things worked, you should now have a summary.txt and dump.csv file in the same directory filled with tracing information. Good luck.
Which ADONETDIAG.dll did you register with those steps? I followed these (they appear to be the ones Microsoft published that everyone else uses for their guides) and found them to work with one gotcha.
The one gotcha I discovered is that there can be multiple copies of ADONETDIAG.dll installed on your machine - and the DLL you register matters! For example, my machine has one installed in the following locations
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\AdoNetDiag.dll
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\AdoNetDiag.dll
C:\Windows\Microsoft.NET\Framework\v2.0.50727\AdoNetDiag.dll
C:\Windows\Microsoft.NET\Framework\v4.0.30319\AdoNetDiag.dll
All of the steps I've found direct you to register \Framework\v2.0.50727\AdoNetDiag.dll. When I tried this I was not getting any logs (my .etl files capture 1 event, the trace start, and never grow beyond 8KB).
Trial and error led to discovering \Framework64\v4.0.30319\AdoNetDiag.dll working for me.

Ado.net firebird commit insert new lines to table

I'm adding new lines to a database for our company's "order list" for each order created, using the firebird ado.net client. The code i've written works fine for listing items, but inserting new ones doesn't appear elsewhere (e.g. in flamerobin). What I think is happening is that the transaction isn't being committed, seeing as it's recognised within my code (can't add duplicate values).
Code:
using (FbConnection fbCon = new FbConnection)
{
fbCon.Open();
***command w/ parameterised command string***
using (FbTransaction fbTrans = fbCon.BeginTransaction())
using FbCommand orderCommand = new FbCommand(cmdString, fbCon, fbTrans)
{
***Adding parameters and values***
try
{
int recordsAffected = orderCommand.ExecuteNonQuery();
fbTrans.Commit();
}
catch (FbException E)
{
fbTrans.Rollback();
fbCon.Close();
throw E
}
}
recordsAffected returns 1 but I am not able to see the updated values in flamerobin or the db management program. Am i missing something?
If anyone else runs into this problem, it's in Visual Studio's debugging settings. https://visualstudiomagazine.com/blogs/tool-tracker/2012/05/dealing-with-local-databases-or-why-your-updates-dont-stick.aspx explains pretty clearly, but basically VS makes a copy of your database in bin/Debug of your project (Ostensibly to not mess with your data) but if you actually need to use/view the data externally, either link your external application to the debug database (e.g. flamerobin). You may also need to set your project database settings to Copy if Newer if you want your updates to stay, as the default setting copies the database into the folder each time you run your c# app.

Update table (rows) Access 2007 and C# 2010

So this is probably the most naive question but that is what questions are for I guess;
Then, my issue is that I have no idea on how to connect Visual C# Express 2010 to Access 2007 and do the typical insert, update, delete, search in an application in C#, I have just learned the basics (finished a console tutorial, which I believe is more than enought, having previous background of VB6 using access 97), and I have been searching here and in the web, but the only thing I could find where the msdn tutorials which I dind't find really clear.
So in my app I just need to link comboboxes, query those values to obtain new ones, do calculations and then store in arrays (and maybe show these in datagrids as well as edit them from said datagrids, which is a bit more complicated I guess) and finally store them in various tables, but I haven't really found a strong (or most likely simple) manual that will guide me to create the typical app insert, update, delete using winforms.
Do you guys have any good links in order to do this?
Thanks.
You can try with this code
Here link about string connection : http://www.connectionstrings.com/access-2007
var query = "...";
var connectionString = "...";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
// The insertSQL string contains a SQL statement that
// inserts a new row in the source table.
using(var command = new OleDbCommand(query))
{
// Set the Connection to the new OleDbConnection.
command.Connection = connection;
// Open the connection and execute the insert command.
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// The connection is automatically closed when the
// code exits the using block.
}
}

What is the best way to connect and use a sqlite database from C#

I've done this before in C++ by including sqlite.h but is there a similarly easy way in C#?
I'm with, Bruce. I AM using http://system.data.sqlite.org/ with great success as well. Here's a simple class example that I created:
using System;
using System.Text;
using System.Data;
using System.Data.SQLite;
namespace MySqlLite
{
class DataClass
{
private SQLiteConnection sqlite;
public DataClass()
{
//This part killed me in the beginning. I was specifying "DataSource"
//instead of "Data Source"
sqlite = new SQLiteConnection("Data Source=/path/to/file.db");
}
public DataTable selectQuery(string query)
{
SQLiteDataAdapter ad;
DataTable dt = new DataTable();
try
{
SQLiteCommand cmd;
sqlite.Open(); //Initiate connection to the db
cmd = sqlite.CreateCommand();
cmd.CommandText = query; //set the passed query
ad = new SQLiteDataAdapter(cmd);
ad.Fill(dt); //fill the datasource
}
catch(SQLiteException ex)
{
//Add your exception code here.
}
sqlite.Close();
return dt;
}
}
There is also an NuGet package: System.Data.SQLite available.
Microsoft.Data.Sqlite by Microsoft has over 9000 downloads every day, so I think you are safe using that one.
Example usage from the documentation:
using (var connection = new SqliteConnection("Data Source=hello.db"))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
#"
SELECT name
FROM user
WHERE id = $id
";
command.Parameters.AddWithValue("$id", id);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var name = reader.GetString(0);
Console.WriteLine($"Hello, {name}!");
}
}
}
I've used this with great success:
http://system.data.sqlite.org/
Free with no restrictions.
(Note from review: Original site no longer exists. The above link has a link pointing the the 404 site and has all the info of the original)
--Bruce
There is a list of Sqlite wrappers for .Net at http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers. From what I've heard http://sqlite.phxsoftware.com/ is quite good. This particular one lets you access Sqlite through ADO.Net just like any other database.
There's also now this option: http://code.google.com/p/csharp-sqlite/ - a complete port of SQLite to C#.
https://github.com/praeclarum/sqlite-net is now probably the best option.
Another way of using SQLite database in NET Framework is to use Fluent-NHibernate.
[It is NET module which wraps around NHibernate (ORM module - Object Relational Mapping) and allows to configure NHibernate programmatically (without XML files) with the fluent pattern.]
Here is the brief 'Getting started' description how to do this in C# step by step:
https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started
It includes a source code as an Visual Studio project.
Here I am trying to help you do the job step by step: (this may be the answer to other questions)
Go to this address , down the page you can see something like "List of Release Packages". Based on your system and .net framework version choose the right one for you. for example if your want to use .NET Framework 4.6 on a 64-bit Windows, choose this version and download it.
Then install the file somewhere on your hard drive, just like any other software.
Open Visual studio and your project. Then in solution explorer, right-click on "References" and choose "add Reference...".
Click the browse button and choose where you install the previous file and go to .../bin/System.Data.SQLite.dll and click add and then OK buttons.
that is pretty much it. now you can use SQLite in your project.
to use it in your project on the code level you may use this below example code:
make a connection string:
string connectionString = #"URI=file:{the location of your sqlite database}";
establish a sqlite connection:
SQLiteConnection theConnection = new SQLiteConnection(connectionString );
open the connection:
theConnection.Open();
create a sqlite command:
SQLiteCommand cmd = new SQLiteCommand(theConnection);
Make a command text, or better said your SQLite statement:
cmd.CommandText = "INSERT INTO table_name(col1, col2) VALUES(val1, val2)";
Execute the command
cmd.ExecuteNonQuery();
that is it.
Mono comes with a wrapper, use theirs!
https://github.com/mono/mono/tree/master/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0 gives code to wrap the actual SQLite dll ( http://www.sqlite.org/sqlite-shell-win32-x86-3071300.zip found on the download page http://www.sqlite.org/download.html/ ) in a .net friendly way. It works on Linux or Windows.
This seems the thinnest of all worlds, minimizing your dependence on third party libraries. If I had to do this project from scratch, this is the way I would do it.
if you have any problem with the library you can use Microsoft.Data.Sqlite;
public static DataTable GetData(string connectionString, string query)
{
DataTable dt = new DataTable();
Microsoft.Data.Sqlite.SqliteConnection connection;
Microsoft.Data.Sqlite.SqliteCommand command;
connection = new Microsoft.Data.Sqlite.SqliteConnection("Data Source= YOU_PATH_BD.sqlite");
try
{
connection.Open();
command = new Microsoft.Data.Sqlite.SqliteCommand(query, connection);
dt.Load(command.ExecuteReader());
connection.Close();
}
catch
{
}
return dt;
}
you can add NuGet Package Microsoft.Data.Sqlite

Categories

Resources