What is the syntax for these two C# Snowflake commands.
Or where can i find it
using (var sfConn = new SnowflakeDbConnection())
{
sfConn.ConnectionString = sConnectionString;
sfConn.Open();
var cmd = sfConn.CreateCommand();
cmd.CommandText = "select top 100 * from RDW_SF.DM_LOAN.DIM_STD_MTHLY_DTM ;";
SnowflakeDbCommand sfcmd = new SnowflakeDbCommand(sfConn.ConnectionString);
SnowflakeDbCommandBuilder sf = new SnowflakeDbCommandBuilder(,)
// cmd.CommandText = "select top 100 * from RDW_SF.DM_LOAN.DIM_ACCT_CNTC;";
// DWLoad_Load_DM_LOAN_DIM_STD_MTHLY_DTM)
var reader = cmd.ExecuteReader();
Console.WriteLine("Retrieving rows from the Query");
SnowflakeDbDataAdapter mySnowflakeDbDataAdapter = new SnowflakeDbDataAdapter(csfcmd);
DataSet myDataSet = new DataSet();
mySnowflakeDbDataAdapter.Fill(myDataSet, "RECS");
sfConn.Close();
DataTable myDataTable = myDataSet.Tables["RECS"];
nCols = myDataTable.Columns.Count;
}
If you're looking for code examples, the snowflake-connector-net repository carries test-cases that exercise its features.
Example of a simple SnowflakeDbCommand
Similar example, with a DataReader
A DataSet and DbDataAdapter example
The Snowflake connector for .NET implements the standard and familiar .NET System.Data interfaces so any example online that uses them (across database types) can be adapted for use with Snowflake with simple SQL-syntax changes (to conform to Snowflake's SQL).
If you're looking for examples on how to perform a connection, you can find an example connection string here.
Related
When one executes a stored proc against MS SQL Server, the recordset comes with some type info. Clients are capable of retrieving that type info, e. g. like this (C#/.NET):
SqlCommand cmd = new SqlCommand("dbo.MyProc", conn);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
ada.Fill(ds);
string ColName = ds.Tables[0].Columns[0].ColumnName;
Type ColType = ds.Tables[0].Columns[0].DataType;
What about nullability of those columns? While it's not knowable in the general case, sometimes it is - for example, when a recordset column comes straight from a table field. If SQL Server is smart enough, it can determine column nullability at least for those cases. Does SQL Server report nullability to the clients in whatever metadata it provides along with the recordset?
Specifically in the .NET client, there is AllowDBNull in retrieved column properties, but in the scenario above it's unreliable - it comes across as true both for columns that came from nullable fields, and for columns that came from nonnullable fields. Is this a limitation of the .NET client, or a shortcoming of the underlying protocol?
This is a limitation of SqlDataAdapter.
The TDS protocol does return this information, as you can see in the specification.
In turn, SqlDataReader will return this information via GetSchemaTable. But it does not make its way to a table filled using a DbDataAdapter.
You can see this with the following code
using(var conn = new SqlConnection(YourConnectionString))
{
conn.Open();
using(var comm = new SqlCommand("select 1", conn))
using(var reader=comm.ExecuteReader())
{
reader.Read();
reader.GetSchemaTable().Dump();
}
using(var comm = new SqlCommand("select case when getdate() = 1 then 1 end", conn))
using(var reader = comm.ExecuteReader())
{
reader.Read();
reader.GetSchemaTable().Dump();
}
}
You will see that the first has AllowDBNull as false and the second as true.
I would advise you in any case to avoid SqlDataAdapter, as it is only actually useful in data-binding UI scenarios. In back-end code, just use a SqlDataReader, or even better: use an ORM such as Dapper or Entity Framework, which will sort out all of this kind of thing for you.
While clicking on add button data is saved in database but after 2-3 times refresh data in database there 2-4 copies of same data is shown.
How to get to fix this?
String cs = ConfigurationManager.ConnectionStrings["MyDBConnectionString1"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("Insert into tblBrands values('" + txtBrandName.Text + "')", con);
con.Open();
cmd.ExecuteNonQuery();
txtBrandName.Text = string.Empty;
}
If you are trying to solve in SQL (assuming from your tags) you could check before inserting using:
if not exists (select * from tblBrands where ...)
Build your where clause based on your criteria - what would you consider duplicate entry
More info on exists in Microsoft Docs
I am using ServiceStack.Ormlite for SQL Server and just updated from 3.9.71 to 4.0.33.0 and now transactions for direct commands are failing. I can get ORMlite transactions working or direct commands, but not both.
The complication is I am doing some very complicated DB commands and since Sql.In() for a large list of GUIDs is massively slow I have a workaround which uses db.CreateCommand() and then passes the GUID list in as a custom table type.
Thus I need a single transaction to span across ORMLite commands and direct db commands. The following code used to work and now it fails.
For instance the following code used to work. I now get errors saying that the CreateCommand() should use the transaction. When I directly try then I get the indicated cast exception:
using (var db = DB.Connection.OpenDbConnection())
{
using (var transaction = db.OpenTransaction())
{
// Some ORMLite code
db.Delete<SomeType>();
using (var command = db.CreateCommand())
{
// Direct DB command
command.CommandText = "Delete from SomeTable where ...";
command.Parameters.Add(GUIDList)
command.ExecuteNonQuery();
}
}
}
Clarification:
In the code OpenTransaction() will work for the OrmLite code, but fail on the CreateCommand code. BeginTransaction() will fail for the OrmLite code.
The actual error is at command.ExecuteNonQuery(): ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.
To use Transactions in OrmLite you should use the OpenTransaction() API, e.g:
using (var trans = db.OpenTransaction())
{
//...
}
I've added a couple new API's to be able to use an OrmLite transaction with a raw ADO.NET IDbCommand in this commit.
Use a managed OrmLite DB Command
Use a managed OrmLite command with OpenCommand() which will automatically prepopulate the DB command with the current transaction, e.g:
using (var trans = db.OpenTransaction())
using (var command = db.OpenCommand())
{
command.CommandText = "Delete from SomeTable where ...";
}
Manually assign underlying DB Transaction
When using the underlying ADO.NET IDbCommand you will need to also manually assign the Transaction to the command yourself, i.e:
using (var trans = db.OpenTransaction())
using (var command = db.CreateCommand())
{
command.Transaction = trans.ToDbTransaction();
command.CommandText = "Delete from SomeTable where ...";
}
The ToDbTransaction() extension method lets you access the underlying ADO.NET IDbTransaction which is required when using the underlying ADO.NET IDbCommand.
Both of these new API's are available from v4.0.34+ that's now available on MyGet.
Here is my suggestion that works.It is based on previous answers
IDbConnection conn = DB.Connection;
IDbCommand cmd = conn.CreateCommand();
using (IDbTransaction transaction = conn.OpenTransaction())
{
//ADO.NET code
cmd.Transaction = transaction.ToDbTransaction();
cmd.CommandText = "...Some sql text";
cmd.executeNonQuery();
// Some ORMLite code
conn.Delete<SomeType>();
}
hey guys i want to fetch 3 tables in 1 single ado.net call from my ms access database, however i get an error when i am trying to do that
when i change my sql query to just fetch 1 table, my code works fine
can anyone let me know how to achieve this with ms access? because i have been doing this with sql server since ages without any problems. perhaps access does not support multiple result sets? i have not worked much with access. please help. below is my code for reference:
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DatabaseFile.mdb;Persist Security Info=True");
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM Table1; SELECT * FROM Table2; SELECT * FROM Table3;", con);
DataSet ds = new DataSet();
da.Fill(ds);
UPDATE: guys this does not look possible. so i had to write really stupid code to get what i wanted, complete waste of computing resources:
DataSet ds = new DataSet();
ds.Tables.Add(new DataTable("Table1"));
ds.Tables.Add(new DataTable("Table2"));
ds.Tables.Add(new DataTable("Table3"));
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DatabaseFile.mdb;Persist Security Info=True");
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM Table1;", con);
da.Fill(ds, "Table1");
da.SelectCommand.CommandText = "SELECT * FROM Table2;";
da.Fill(ds, "Table2");
da.SelectCommand.CommandText = "SELECT * FROM Table3;";
da.Fill(ds, "Table3");
as far as I know, if your data reside inside the Access mdb, you cannot have multiple data sets. If instead you use Access to connect to an external data source, you could use pass-through queries and do that, but I do not think this is your case. (see http://support.microsoft.com/kb/126992)
Is it possible (in Vb.Net 2005), without manually parsing the dataset table properties, to create the table and add it to the database?
We have old versions of our program on some machines, which obviously has our old database, and we are looking for a way to detect if there is a missing table and then generate the table based on the current status of the table in the dataset. We were re-scripting the table every time we released a new version (if new columns were added) but we would like to avoid this step if possible.
See this MSDN Forum Post: Creating a new Table in SQL Server from ADO.net DataTable.
Here the poster seems to be trying to do the same thing as you, and provides code that generates a Create Table statement using the schema contained in a DataTable.
Assuming this works as it should, you could then take that code, and submit it to the database through SqlCommand.ExecuteNonQuery() in order to create your table.
Here is the code:
SqlConnection con = new SqlConnection("Data Source=.;uid=sa;pwd=sa123;database=Example1");
con.Open();
string sql = "Create Table abcd (";
foreach (DataColumn column in dt.Columns)
{
sql += "[" + column.ColumnName + "] " + "nvarchar(50)" + ",";
}
sql = sql.TrimEnd(new char[] { ',' }) + ")";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
using (var adapter = new SqlDataAdapter("SELECT * FROM abcd", con))
using(var builder = new SqlCommandBuilder(adapter))
{
adapter.InsertCommand = builder.GetInsertCommand();
adapter.Update(dt);
}
con.Close();
I hope you got the problem solved.
Here dt is the name of the DataTable.
Alternatively you can replace:
adapter.update(dt);
with
//if you have a DataSet
adapter.Update(ds.Tables[0]);