Connect to SQL Anywhere - dblgen12.dll not found - c#

I try to connect to a SQL Anywhere 12 database with the iAnywhere.Data.SQLAnywhere.v4.0.dll.
On the database server this connection is successful, however on any other PC's I get the error Language Ressourcefile not found<dblgde12.dll, dblgen12.dll>.
Orginal (german) error message here, unfortunately even on my english system the error message is in german.
I have found these files on the installation directory of the sql server, but I don't know what to do with them, since I can't install them in the gac.
The Connection:
iAnywhere.Data.SQLAnywhere.SAConnection connection = new SAConnection();
connection.ConnectionString = new SAConnectionStringBuilder(){ DatabaseName = "123", UserID = "123", Password =
"123", ServerName = "123" , Host = "10.11.11.10"}.ConnectionString;
connection.Open();
Thanks

After some more time asking google I found the following question asked:
http://sqlanywhere-forum.sap.com/questions/19449/cannot-find-the-language-resource-file-dblgen12dll
The accepted answer links to http://dcx.sybase.com/index.html#1201/en/dbadmin/da-install-s-5107108.html, where is explained where the dll looks for the files.
On Windows, SQL Anywhere searches the following paths relative to each
location in the preceding list:
.
..
.\bin32 and ..\bin32 (32-bit programs only)
.\bin64 and ..\bin64 (64-bit programs only)
.\java (for Java-related files)
..\java (for Java-related files)
.\scripts (for SQL script files)
..\scripts (for SQL script files)
The necessary files can be found here http://dcx.sybase.com/index.html#1201/en/dbprogramming/deploying-adonet-deploy.html
All these files can be found on a SQL Anywhere Installation 12 in the following directory: C:\Program Files\Sybase\SQL Anywhere 12

Extra info
You need to put dblgde12.dll and dblgen12.dll in the project folder \bin\debug
As for the connection string i'm using this one.
SAConnection conn = new SAConnection("ENG=LXDBSRV;DBN=database;UID=username;PWD=password;POOLING =FALSE;LINKS=tcpip{Host=PC-g};CS=utf-8");
Other way is :
SAConnection conn = new SAConnection("DSN=LXSYDSN;UID=username;PWD=password;DBF=c:\\LxOffice.db");

Related

Cannot add sqlite db file to Word adding deployment

I developed C# Word add-in which populates some data from database file. It is working fine while I am running through Visual studio running. But if I publish it cannot connect to database.
Here is how I get data from database:
SQLiteConnection con = new SQLiteConnection(ThisAddIn.connectionString);
con.Open();
var command = con.CreateCommand();
command.CommandText =
#"
SELECT *
FROM JK
WHERE article_number = $article_number
";
command.Parameters.AddWithValue("$article_number", textFromDoc);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var title = reader.GetString(2);
var article = reader.GetString(3);
ResultForm resultForm = new ResultForm();
resultForm.setArticle(title, article);
resultForm.ShowDialog();
}
}
public static string connectionString = #"Data Source=E:\projects\c#\TBPWordAddin\WordAddIn1\codexes.db";
Am I doing something wrong or do I need to include file in another way? Any help will be appreciated.
Also I tried publishing using Visual studio installer and it connected to database but the add in didnot lounched on other computers.
The connection string contains an absolute path which can be changed after publishing an application. I'd suggest using a relative path instead - in that case you will be able to find the Db easily. You may check out the following threads for more information on such kind of issues:
Connection string with relative path to the database file
How to give relative path of connection string or data source in windows form application
But if I publish it cannot connect to database.
Make sure the Db path (see the connection string) corresponds to the hardcoded value in the application used.

Backup localDB database in ClickOnce

I've created a WPF 4.5 .NET application it with a database backup feature. The functions and the backup works fine when debugging but when I publish it in ClickOnce and install it in target machine everything works except the backup won't work because ClickOnce obfuscate the app folder location so it becomes too long for the backup statement to work! Is there a way to make the backup statement shorter? here's my code and the error I get:
code:
SaveFileDialog sfd = new SaveFileDialog();
string stringCon = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\whdb.mdf;Integrated Security=True";
string dbPath = Application.StartupPath + #"\whdb.mdf";
if (sfd.ShowDialog() == DialogResult.OK)
{
using (SqlConnection conn = new SqlConnection(stringCon))
{
string backupStmt = string.Format(#"backup database #whdb to disk='{0}' WITH INIT ", sfd.FileName);
using (SqlCommand sqlComm = new SqlCommand(backupStmt, conn))
{
sqlComm.Parameters.AddWithValue("#whdb", dbPath);
conn.Open();
sqlComm.ExecuteNonQuery();
conn.Close();
}
}
)
************** Exception Text **************
System.Data.SqlClient.SqlException (0x80131904): Invalid database name 'C:\Users\Abubaker\AppData\Local\Apps\2.0\52WR4JTO.12O\D6M4D7OQ.Z3D\sa3a..tion_fef19ab42c2b8f22_0001.0000_9fc10c82bbf23ed2\whdb.mdf' specified for backup or restore operation.
BACKUP DATABASE is terminating abnormally.
The whdb.mdf database file is create by another existing application? And your WPF application is only for Backup the existing whdb.mdf database?
I also struggled for days for this kind of situation. I tried |Data Directory| and Application.StartupPath and some other approaches as you've found as well.
However, I finally chose this way and successfully launched(published) my service.
According to my experience, |Data Directory| indicates different places depending on circumstances, in other word, it's not always same..
And I could read (SQL Select command) database with |Data Directory| setting in connectionString but couldn't insert or update the database. You can find some people are having this difficulties by searching on interent. I think when we set |Data Directory| in connectionString, the database plays a role as read-only data file like the nuance of |Data Directory|.
Instead the |Data Directory|, I chose more obvious path which indicates always same place(directory).
#"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + Environment.GetEnvironmentVariable("APPDATA") + #"\whdb.mdf;Integrated Security=True";
Above Environmental variable indicates always C:\Users\Abubaker\AppData\Roaming directory.
For this scenario, you're needed to create the whdb.mdf database file with above path first.
And further, you may create your own additional directory like mybackup with the connectionString as,
#"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + Environment.GetEnvironmentVariable("APPDATA") + #"\mybackup" + #"\whdb.mdf;Integrated Security=True";
I couldn't find the official document from Microsoft how the |Data Directory| works but failed. Understanding |Data Directory| would be your key, otherwise, there's alternative way like my case.
My case is also WPF, ClickOnce, SQL Database (it was localDB but I changed to normal SQL Database because of remote networking)
Solved the problem by adding Initial catalog=whdb in the connection string and then replace the full path Application.StartupPath + #"\whdb.mdf" with just the database name "whdb"!

Changes do not get saved permanently into SQL Server Compact edition

I am having a weird problem. I am using vs2012 to connect to SQL Server CE and executing some insert queries.
public void EstablishConnection()
{
connection = new SqlCeConnection("Data Source=DataDump.sdf;Persist Security Info=False;");
try
{
connection.Open();
Console.WriteLine("Connection Successful");
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
public void AddRecord(string link,string content)
{
int num = 0;
var command = new SqlCeCommand("INSERT INTO Webpages(Link,Data) VALUES('"+link+"','"+content+"');",connection);
num = command.ExecuteNonQuery();
Console.WriteLine("Command Successful rows affected"+num);
var cmd2 = new SqlCeCommand("SELECT * FROM Webpages",connection);
SqlCeDataReader reader = cmd2.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader[0]);
Console.WriteLine(reader[1]);
}
}
However I am having the problem that once VS is closed and when later I open it to display the data, the data is gone as it was never saved
How is that possible when it is clear then it executed the query?
It is a common scenario.
You have your database file listed between your project items.
Its property Copy to Output directory is set to Copy Always.
Now, you run your debug session with VS. The compile is succesfull and VS copies your sdf file from the project folder to the current output directory (BIN\DEBUG).
Your code runs smoothly and inserts the data in your database file (on the output directory).
You stop and restart the debug session to fix something, but, at restart, the VS recopies the empty file from the project folder to the output directory.
To break this circle, set Copy to Output Directory to Copy Never (or Copy if Newer)
EDIT Another source of confusion is due to the use of SERVER EXPLORER to view the contents of your database file. If the server explorer use a connection string that points to the database file in the project folder you never see the changes made to the database file in the Output Directory.
You should create two connections in Server Explorer, one named DEBUG DataDump that points to PROJECTFOLDER\BIN\DEBUG. You could use this connection to check the data inserted during debug or for other debugging tasks. Another one, called DISTRIBUTION DataDump, points to the file in the project folder and you make here your schema changes needed for the distribution of your app.
Said that, keep in mind that your code has a BIG problem. It is called Sql Injection
A parameterized query will avoid quotations problems and remove the Sql Injection
int num = 0;
var command = new SqlCeCommand("INSERT INTO Webpages(Link,Data) " +
"VALUES(#lnk, #cnt)",connection);
command.Parameters.AddWithValue("#lnk", link);
command.Parameters.AddWithValue("#cnt", content);
num = command.ExecuteNonQuery();
set Copy to Output Directory property as Copy if newer for your sdf file
it seems now you have set it as copy always which result :
The database file is copied from the project directory to the bin
directory every time that you build your application. Any changes made
to the data file in the output folder are overwritten the next time
that you run the application.

Why do I get "Database already exists" when I'm using "AttachDbFileName" when I move the solution to another directory?

I have an application JigSaw that uses a database TopScores.mdf which it is not included in the project. What I want to do is make the application find the database in application's folder. So for example if I move the .exe file and the database file from debug folder to desktop the application should use the database from desktop and not search for it in debug folder.
If I let them in debug folder there is no problem and the connection to database is established, but when I put them on desktop I get this :
Unhandled exception has occurred in your application. If you click Continue, the application will ignore this error and attempt to continue. Database 'D:\Programing\Projects Visual Studio 2010\JigSaw\JigSaw\bin\Debug\TopScores.mdf' already exists. Choose a different database name. Cannot attach the file 'C:\Users\Addy\Desktop\Jigsaw\TopScores.mdf' as database TopScores.mdf
My connection string is :
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
string connString = #"server =.\sqlexpress; Database=TopScores.mdf; trusted_connection=TRUE; AttachDbFileName= "+#appPath+#"\TopScores.mdf";
conn = new SqlConnection(connString);
Sorry for my bad english :(
This is happening because you don't leverage the User Instance=true; when you connect to it. You literally told SQL Server to attach the database to the running SQL instance from the directory you first loaded it from.
Detach the database by hand from the running SQL instance, change your connection string to use User Instance=true;, run it from the Debug folder, and then run it from the Desktop, and you'll see success.
change setting in your connection string from "Database=TopScores.mdf" to something different, say: "Database=TopScores_brand_new_connection" do not create/delete/rename files in any file/database server. do it in the connection string only. Do not add dots, extensions etc.
just rename ur .mdf file ... eg. from MNGMT.mdf to M_1(something you want). change your contact string...hope so it will help u.. in my case it is executing correctly on other pc as well as in my pc.. just copy your .mdf file into you project ..
string dbPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\M_1.mdf";
string myServer = Environment.MachineName;
DataTable servers = SqlDataSourceEnumerator.Instance.GetDataSources();
for (int i = 0; i < servers.Rows.Count; i++)
{
if (myServer == servers.Rows[i]["ServerName"].ToString()) ///// used to get the servers in the local machine////
{
if ((servers.Rows[i]["InstanceName"] as string) != null)
servername = (servers.Rows[i]["ServerName"] + "\\" + servers.Rows[i]["InstanceName"]);
else
servername = ""+servers.Rows[i]["ServerName"];
}
}
connetionString = "Data Source=" + servername + ";AttachDbFilename=" + dbPath + ";Integrated Security=True;Pooling=False;User Instance=True";

Microsoft ACE OLEDB connection creating empty Excel when there are 166,110 rows

I am programming in C# and using an oledbconnection. This is the standard connection string e.g.
using (OleDbConnection conn = new OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
saveFilenameAndLocation +
";Extended Properties='Excel 12.0 Xml;HDR=Yes'"
))
This works successfully and creates my Excel spreadsheet full of the correct data, when the number of rows isn't excessive. However, whenever the number of rows in the spreadsheet increases to a large size (e.g. currently it is failing on 166,110 rows), it produces an empty spreadsheet with the worksheet tab name set to A266FF2A662E84b639DA.
It is not possible to rewrite this so that it doesn't use an OLEDB connection, any ideas why it doesn't work when the row size increases?
I am not sure about your application environment, but I have seen this when generating Excel files from an ASP.NET app.
Once the amount of data exceeds a certain size (~1 MB in my experience), the provider will attempt to create a temp file while generating the output. I have come across this issue using the 32-bit provider on 64-bit systems. If you are running under a service account with this configuration then the location where these files are created is
C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.MSO
This location, however, is only accessible to administrators and SYSTEM by default, and if the provider is running under a non-privileged account and is unable to create the temp file it will fail silently and just return its default "empty file" with the A266FF2A662E84b639DA worksheet.
What you need to do is grant the account of the application that runs under (for example Network Service or IIS AppPool\) read/execute/list contents permissions along the path "C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files" and then full access to the Content.MSO folder itself.
If the provider matches the bitness of your system then I suspect you need to perform the process above for C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.MSO instead, although I have never tested this.
I want to extend my thanks to sysinternals for providing procmon that helped me troubleshoot this issue.
One of the employees at my company ran across this problem today. The solution was simple: She had only 20MB free space left on C: - If you free up enough space on your system drive, it should solve the problem.
Windows Server 2012R2
Check also if folder INetCache exists and this folder has full perimissions C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\INetCache
After 10 hours I found the solution.
Every 1000 rows we have to close and reopen the connection.
Sample is here.
string createTable = "Create Table [Sheet_X] (field1 char(255), field2 char(255),field3 char(255));";
OleDbCommand cmd = new OleDbCommand(createTable, conn);
conn.Open();
cmd.ExecuteNonQuery();
var counter = 0;
foreach (var item in items)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
string insertdata = "insert into [Sheet_X] (field1,field2,field3) values('value1','value2','value3');";
counter++;
if (counter >= 1000)
{
counter = 0;
conn.Close();
}
}
If it still don't work after tried all above solutions, try this one, it worked for me:
In the "Advanced settings" of your IIS Application Pool, Change the value of "Load User Profile" from "False" to "True".

Categories

Resources