My query is when I install this application on some system with SQL Server install I should get all databases in a combobox.
Login image
In the first combobox (Select database type) we have option to select the SQL Server. When I will select the SQL Server, I wish that the available instance of the database like root or something else should come in second combobox. And we should get the database name of the all database in the 3rd combobox
To list the instances of Sql Server you can use:
SQLCMD -L
By executing it in a process and collect the output.
For listing of the databases you can use this select statement:
SELECT name FROM master.dbo.sysdatabases
or
EXEC sp_databases
The problem is, you need to be logged in to the database to get the list of databases which the logged in user is allowed to see. I think you need to consider it in your application.
Related
I have an Azure SQL (S3) geo-replicated read-only database.
My problem is that when I query the Read-only from VisualStudio I can see the Query is hitting the Master database.
I expect the Query to hit the Read-only database.
But if I run the same Query from SMSM connected to Read-only then I can see the Read-only database is hit. This works as expected.
To see the last Query in each database I use following SQL.
SELECT execquery.last_execution_time AS [Date Time], execsql.text AS [Script] FROM sys.dm_exec_query_stats AS execquery
CROSS APPLY sys.dm_exec_sql_text(execquery.sql_handle) AS execsql
ORDER BY execquery.last_execution_time DESC
Question
Why is my c# (entityframework 6.0) code not showing in the Read-only database?
Background
The goal is to have a read-only SQL to handle the external API load so the Master SQL is not to load.
In Azure Portal, I created a Geo-Replication SQL in the same region as Master.
The connection string is set to Read-only database.
I tried[ApplicationIntent=ReadOnly] flag in connectionstring with no success.
The problem was old format on the connectionstring "User ID".
I used "username#server". The #server was pointing to the server of Master Sql.
I changed to only "username" and now it works.
I am trying to copy all table data from server to my local database, like
INSERT INTO [.\SQLEXPRESS].[Mydatabase]..MYTable
SELECT *
FROM [www.MYSite.com].[Mydatabase]..MYTable
www.MYSite.com having SQL LOGIN ID XYZ AND PASSWORD 1234
but I get an error:
Could not find server 'www.MYSite.com' in sys.servers.
Verify that the correct server name was specified. If necessary,
execute the stored procedure sp_addlinkedserver to add the server to sys.servers.
I want to copy all the data from Mytable of www.MYSite.com to Mytable of .\SQLExpress.
How to resolve it? Please help.
Update :
I am using Microsoft Sync Framework 2.0 to sync all data from www.MYSite.com to .\SQLExpress and vice versa, but in a one condition I want to copy data from www.MYSite.com to .\SQLExpress without sync framework
Please Note I am passing those SQL Statement using C#..
When you specify a database on another server, like this:
SELECT *
FROM [www.MYSite.com].[Mydatabase]..MYTable
... the server name needs to be one that the database server was previously configured to recognize. It needs to be in the system table sys.servers.
So, you need to configure your SQLExpress instance to "know about" that server.
You can do this in code, with the stored procedure sp_addlinkedserver. You can learn more about it here.
Or, you can do it through SSMS:
I hope the below information will help you:
Using SQL Server Management Tools you can use the Import Feature.
Connect to your SQL instance server.
Select your database schema.
Right click Tasks > Import.
and follow wizard instructions.
I created a windows forms application in C #, and a database MS SQL server 2008 Express, and I use LINQ-to-SQL query to insert and edit data.
The database is housed on a server with Windows Server 2008 R2 (standard edition). Right now I have the application running on five different computers, and users are authenticated through active directory.
One complaint reported to me was that sometimes when different data is entered and submitted, the same data do not appear in the listing that contains the application. I use try catch block to send the errors but errors do not appear in the application; but the data simply disappear.
The id of the table records is an integer auto-increment. As I have to tell them the registration number that was entered I use the following piece of code:
try{
ConectionDataContext db = new ConectionDataContext();
Table_Registers tr = new Table_Registers();
tr.Name=textbox1.text;
tr.sector=textbox2.text;
db.Table_Registers.InsertOnSubmit(tr);
db.SubmitChanges();
int numberRegister=tr.NumberRegister;
MessageBox.Show(tr.ToString());
}
catch{Exception e}
I wonder if I'm doing something wrong or if you know of any article on the web that speaks how to insert data from different clients in MSSQL Server databases, please let me know.
Thanks.
That's what a database server DOES: "insert data simultaneously from different clients".
One thing you can do is to consider "transactions":
http://www.sqlteam.com/article/introduction-to-transactions
Another thing you can (and should!) do is to insure as much work as possible is done on the server, by using "stored procedures":
http://www.sql-server-performance.com/2003/stored-procedures-basics/
You should also check the SQL Server error logs, especially for potential deadlocks. You can see these in your SSMS GUI, or in the "logs" directory under your SQL Server installation.
But the FIRST thing you need to do is to determine exactly what's going on. Since you've only got MSSQL Express (which is not a good choice for production use!), perhaps the easiest approach is to create a "log" table: insert an entry in your "log" every time you insert a row in the real table, and see if stuff is "missing" (i.e. you have more entires in the log table than the data table).
With SQL Server I run this query with no problem...
SELECT SUM(Esi) AS Dispo
FROM [mdb].[dbo].[Query1] AS A
INNER JOIN [mdb2].[dbo].[TieCol] as B ON A.Alias=B.IDAlias
WHERE Alias LIKE 'SETUP%'
I join two tables that reside in two different databases (mdb and mdb2).
But how can I do it in my .NET application?
When I need to use this statement
string cmdText = "SELECT SUM(Esi) AS Dispo
FROM [mdb].[dbo].[Query1] AS A
INNER JOIN [mdb2].[dbo].[TieCol] as B ON A.Alias=B.IDAlias
WHERE Alias LIKE 'SETUP%'";
this.OP = new SqlConnection(ConfigurationManager.ConnectionStrings["mdb2"].ConnectionString);
SqlCommand sqlCommand = new SqlCommand(cmdText, this.OP);
I can't execute it, since this.OP is the connection to mdb2... And for mdb?
How can I connect to both databases simultanously?
The SQL connection is to the server - the Initial catalog in a connection string behaves like use - it sets the default DB.
So your 3 part SQL query should work as is. So possibly
Make sure that the SQL login used by your app (or the account of your AppPool if using Web and Integrated Security) has the necessary access to both databases. (use RunAs on SQL Enterprise Manager as this account and try to run the query)
You might try escaping [Alias]
Also, if there is coupling between mdb1 and mdb2 (e.g. SPROCS in mdb1 use tables in mdb2 etc), for ease of maintenance, you might consider adding views in mdb1 for mdb2 objects. This allows for easy identification of cross-database dependencies. In this case, your query can use views which look like they are in the same database, although the underlying dependency on mdb2 is still there.
I'm not sure if there is a way to do this within the connection string. But you can probably do it using a four part reference to the table: [server].[database].[table].[column].
Your C# application only need to connect to one database server for this query.
Say your C# application connect to [mdb]. Database [mdb2] should b linked server in database [mdb].
Since you can run that query in sql server, so there must be one sql server connected to both databases. use that sql server in your C# connection string. That's it!
i have an idea to call two values from two different databases and comapre them in one statement? is it possible?
i am working with c# and MS-SQL
Yes.
For MSSQL you can add the database name in front of your table. You normally have 4 namespaces you can use
[Server name].[database name].[owner].[table_name]
So if you want to compare two values in the one statement you should only need to join across the tables by placing the database name in front of the table name.
If the databases are on different servers then you will need to create a linked server to the side which will run your SQL so that its aware of the other sql server. You can add linked servers in Management studio or via SQL using something like sp_addlinkedserver
You may do cross database joins to compare these values:
SELECT
db1.Value as value1,
db2.Value as value2
FROM
[database1].dbo.MyTable1 as db1
INNER JOIN
[database2].dbo.MyTable as db2
ON
/* insert join clasue */
There are a few possibilities here depending on your setup. If your databases are different SQL Server installations then you will want to look at sp_linkedserver first. Once you have the ability to see both databases using the same login you could just execute the following query where db1 and db2 are the databases, dbo is the owner and tab1 and tab2 are the respective tables.
SELECT a.col1
FROM db1.dbo.tab1 a, db2.dbo.tab2 b
WHERE a.col1 = b.col2
If you should happen to lack the SQL Server permissions to create a linked server, you could create connections to each server and your client could attach to the servers using Microsoft JET library, and then you could perform the heterogeneous join client-side.