I'm following http://www.mssqltips.com/tip.asp?tip=1910 and the following code actually does work, but takes about 90 seconds to run. The database schema has less than 10 (fairly straightforward) tables. I'm not sure why it takes so long. Any suggestions on how to debug this?
var host = "192.168...";
var user = "username";
var pass = "password";
var srcDbName = "srcDbName";
var dstDbName = "dstDbName";
var server = new Server(new ServerConnection(host, user, pass));
var srcDb = server.Databases[srcDbName];
var dstDb = new Database(server, dstDbName);
dstDb.Create();
var transfer = new Transfer(srcDb);
transfer.CopyAllTables = true;
transfer.Options.DriAll = true;
transfer.Options.ContinueScriptingOnError = false;
transfer.DestinationDatabase = dstDbName;
transfer.DestinationServer = server.Name;
transfer.DestinationLoginSecure = false;
transfer.DestinationLogin = user;
transfer.DestinationPassword = pass;
transfer.TransferData();
I think that you should leave this code alone. You cannot improve it. AS I understand from the question you want to transfer tables/schema from one database to other .
Below options that I suggest :
Linked Servers
From SQL 2000 you should be able to connect directly to other database as a linked server. In the pros column this kind of direct access can be easy to work with if you don't have any other technical skills such as DTS or SSIS, but it can be complex to get the initial set-up right and there may be security concerns/issues.
DTS
DTS is packaged with SQL 2000 and is made for this kind of a task. If written correctly, your DTS package can have good error-handling and be rerunnable/reusable.
SSIS
SSIS is actually packaged with SQL 2005 and above, but you can connect it to other databases. It's basically a better version of DTS.
Related
I have a WinForms database driven application that I want to make it work in online/offline mode using Dotmim sync framework that I find an article by their author here.
The documentation for the library is here
this is my code to sync the two SQL Server databases one is localdb and the other one is now on the SQL Server Management Studio for the testing purpose:
string connect = #"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=bright_square_db;Integrated Security=SSPI;AttachDBFilename=D:\Folder\project_file\bright_square_db.mdf";
string constring = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlSyncProvider serverProvider = new SqlSyncProvider(constring);
SqlSyncProvider clientProvider = new SqlSyncProvider(connect);
SyncAgent agent = new SyncAgent(clientProvider, serverProvider, new string[] { "I have listed all the tables here" });
var progress = new SynchronousProgress<ProgressArgs>(s => MessageBox.Show($"{s.Context.SyncStage}:\t{s.Message}"));
var syncContext = await agent.SynchronizeAsync(progress);
MessageBox.Show(syncContext.ToString());
But, when I try to run the code. I am getting this error
The columns that indicated in the error are for a table that created by the sync process named "scope_info" inside the SQL Server database.
I have solved the problem by swapping the client and server connection string link in the third and fourth line of the above code. I don't know what exactly cause the problem, but lastly this changed makes the code work for me.
I have two SQL servers that are being load balanced - AlwaysOn. Only the second one of these servers is supposed to be used for crystal reports. I would like to access the second SQL server using the readOnly flag in the connection string: ApplicationIntent=ReadOnly
In my C# class I am running the crystal reports based on ConnectionInfo()
var myConnectionInfo = new ConnectionInfo();
Tables myTables = reportDocument.Database.Tables;
for (int i = 0; i < myTables.Count; i++)
{
var myTable = myTables[i];
var myTableLogonInfo = myTable.LogOnInfo;
myConnectionInfo.ServerName = 'serverName';
myConnectionInfo.DatabaseName = 'databaseName';
myConnectionInfo.UserID = 'userId';
myConnectionInfo.Password = 'password';
myTableLogonInfo.ConnectionInfo = myConnectionInfo;
myTable.ApplyLogOnInfo(myTableLogonInfo);
}
I haven't found a way to set ApplicationIntent=ReadOnly though. Is this supposed to be done setting myConnectionInfo.Attributes? Unfortunately I haven't found an answer on this yet but unanswered questions:
https://archive.sap.com/discussions/thread/3861287
https://archive.sap.com/discussions/thread/3791155
Unfortunately I did not find a way to use the flag ApplicationIntent=ReadOnly in my posted code snippet.
What I ended up doing:
Instead of using the load balancer IP address (or host name), I am using the IP address of the reporting server directly.
I couldn't find any written documentation whether or not one can use ApplicationIntent=ReadOnly.
Rather than using ConnectionInfo, you could use System.Data.SqlClient.SqlConnectionStringBuilder, which has a settable ApplicationIntent property.
I have a program where on a click event SQL will open and connect to a server instance and database however. I am wanting to make to program a bit more dynamic and allow user input but I am struggling to get this to work as wanted.
The old code is as follows and this works:
Process.Start("ssms.exe", "-S .\\SQLEXPRESS -d master -E ");
I tried the following but this just opens SQL but states will not use the details enterd to connect to a database.
Process.Start("ssms.exe", DataBaseNameInput.Text);
Edit
The error SQl shows is The following files where specified on the command line: These files could not be found and will not be loaded.
For this you would need to use SqlConnectionStringBuilder(), this allows you to build a string whitch can then be passed.
var Connection = new SqlConnectionStringBuilder();
Connection.DataSource = ServerNameTextBox.Text;
Connection.InitialCatalog = DatabaseTextbox.Text;
Connection.UserID = UserNameTextBox.Text;
Connection.Password = PasswordTextBox.Text;
var connString = Connection.ConnectionString;
This was done by the following information
Ssms Utility
I have developed a program (C#) which creates a SQL database using this code:
string SQLCreation = "IF NOT EXISTS (SELECT * FROM master..sysdatabases WHERE Name = 'x') CREATE DATABASE x";
SqlConnection PublicSQLDBCreationConnection = new SqlConnection(connectionString);
SqlCommand PublicSQLDBCreation = new SqlCommand(SQLCreation, PublicSQLDBCreationConnection);
try
{
PublicSQLDBCreationConnection.Open();
PublicSQLDBCreation.ExecuteNonQuery();
PublicSQLDBCreationConnection.Close();
}
//'then creates a table and so on
Now I want to have a client application which connects to this database (via LAN) WITHOUT using IP or computer name. How is that possible? Is it possible do this and have a dataset while not mentioning IP Adr. or computer name?
P.S. Don't Worry Guys, I simplified my code just for your view, I have made sure that SQL injection or other attempts won't happen.
Also I have to say that My Reason for not mentioning servername or IP is that I want to mass deploy my Application on many Networks
You could use SqlDataSourceEnumerator to get a list of all Sql Servers that are visible and browsable. This is not a good technique, since you could get an instance that you don't have the right to create a database on it, but you could still try something with that.
var enumerator = SqlDataSourceEnumerator.Instance;
foreach (DataRow row in enumerator.GetDataSources().Rows)
{
var serverName = row["ServerName"];
var instance = row["InstanceName"];
// build a connection string and try to connect to it
}
I have a C# .NET program running an ETL which connects to a DB2 database. Sometimes this database is down, so I'd like to do a health check at the beginning of the application to see if the database is available, without actually calling any stored procedures or pushing any data. Here's an example of the code I'm using now:
OdbcConnection myODBCConnection = new OdbcConnection("DSN=DB2AA;UID=ABCD;PWD=1234;");
OdbcCommand myODBCCommand = new OdbcCommand();
myODBCCommand.CommandType = CommandType.StoredProcedure;
myODBCCommand.CommandText = "{CALL SYSPROC.ABC001(?, ?)}";
myODBCCommand.Parameters.Add("INPUT", OdbcType.VarChar, 500);
myODBCCommand.Parameters["INPUT"] = myString
myODBCCommand.Connection = myODBCConnection
myODBCConnection.Open();
OdbcTransaction myTrans;
myTrans = myODBCConnection.BeginTransaction();
myODBCCommand.Transaction = myTrans;
myTrans.Commit();
myODBCCommand.ExecuteNonQuery();
myODBCConnection.Close();
What's the best way to test this connection without actually pushing any data?
You can simply run some innoccuous select query to check to see if the db is available.
You can try to do something as simple as:
Select 1
Or
Select getdate()
Those simple queries don't even touch any tables but will return only if the rdbms is running.
Note: those examples are for sql server but might work for db2. I haven't had to do a live check on a db2 yet though the similar concept should be doable.
Note 2: after a closer look at your code, all you should really have/need to do is check for success of your odbc connection's .Open() call.