Show used Database in asp.net/c# application [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
We use several machines and databases to develop a asp.net/c# application.
These are activated or deactivated in appsettings.json respectively.
"ConnectionStrings":
{
// Webserver
//"DefaultConnection": "Server=WEBSRV-2019\\SQLEXPRESS;Database=real_app;user id=sa;password=xxxxxxxxxxx;MultipleActiveResultSets=true;"
//"DefaultConnection": "Server=WEBSRV-2019\\SQLEXPRESS;Database=test1;user id=sa;password=xxxxxxxxxxx;MultipleActiveResultSets=true;"
//"DefaultConnection": "Server=WEBSRV-2019\\SQLEXPRESS;Database=test2;user id=sa;password=xxxxxxxxxxx;MultipleActiveResultSets=true;"
// HP-WS10
"DefaultConnection": "Server=HP-WS10\\SQLEXPRESS;Database=real_app;user id=sa;password=xxxxxxxxxxx;MultipleActiveResultSets=true;"
//"DefaultConnection": "Server=HP-WS10\\SQLEXPRESS;Database=test1;user id=sa;password=xxxxxxxxxxx;MultipleActiveResultSets=true;"
//"DefaultConnection": "Server=HP-WS10\\SQLEXPRESS;Database=test2;user id=sa;password=xxxxxxxxxxx;MultipleActiveResultSets=true;"
}
In this example we use Server "HP-WS10" with database "real_app".
It would be nice to see in the application which database is in use.
To show the user if he works with a development environment or the real application.
My problem is that I donĀ“t know where, and how, to read my "ConnectionStrings" in the application.

You can use the SqlConnectionStringBuilder class to parse and extract connection string information.
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder?view=dotnet-plat-ext-7.0
For example:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(myConnectionString);
model.DatabaseServer = builder.DataSource;
return View(model);

Related

Does Dapper open a connection string even if its not used? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
Example:
using (var connection = SqlConnection("DBConnectionString"))
{
C = A.SomeMethod();
}
Does Dapper open up a Connection to the Database in the example case even though there are no calls to the database?
Or is the connection only established when the connection is requested IE:
using (var connection = SqlConnection("DBConnectionString"))
{
C = context.Query("SELECT * FROM UserSecrets");
}

can I use another custom environment variable name instead of GOOGLE_APPLICATION_CREDENTIALS? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
in order to enable multiple credentials to my application - I want to use custom environment variable for each.
how can I set it explicitly?
in GCP docs I see only this default option.
Edit:
the libraries I'm using are Google.Cloud.Storage.V1 and Google.Apis.Dataflow.v1b3
I succeeded by using this
var credentials = GoogleCredential.FromFile(#"c:\certs\sa.json");
var storageClient = StorageClient.Create(credentials);
If you use the default credential creation, only this environment variable is possible
(example in python)
import google.auth
credentials, project_id = google.auth.default()
Now, think at the value that you put in the GOOGLE_APPLICATION_CREDENTIALS env var. It's simply a path to a service account key file. So, you can do that, which is equivalent to the previous command
import google.auth
credentials = service_account.Credentials.from_service_account_file(os.getenv('GOOGLE_APPLICATION_CREDENTIALS'))
And thus, you can use the env var name that you want and create an explicit credential by passing the path to the correct file!
Example in C#:
var credentials = GoogleCredential.FromFile(#"c:\certs\trigger-sa.json");
var storageClient = StorageClient.Create(credentials);

How to align content from a lump of data from sql to web? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a text in SQL like: "I want to eat. I'm hungry."
I use ASP.NET MVC to get data from database:
#Model.content
#Model.content is "I want to eat. I'm hungry."
I want to format text to display, for example, it might come down the line:
I want to eat.
I'm hungry.
Since, you don't prefer saving "\n" in the database, you can just split the entire into an array and loop through each item to display it with breaks. use Split
Something like:
#foreach (var item in Model.content.Split(".")) { #item <br /> }

How to export CSV to Db with headers in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to export csv to db in C#. It works fine when you have a plain text simple text. But in my case I need to parse headers as single row.
There are many options!
use some driver custom functions (depends on your database)
use a [schema.ini](
https://learn.microsoft.com/en-us/sql/odbc/microsoft/schema-ini-file-text-file-driver)
do it manually ...
You can do it manually with while iteration. Before using loop, you should read csv file from path with StreamReader.
For example:
using(var csvReader = new StreamReader(System.IO.File.OpenRead("your file path")))
{
while (!csvReader.EndOfStream)
{
var rows = csvReader.ReadLine();
var columns = rows.Split(';');
if(values.Length > 1)
{
// Your logic getting values to save db
}
}
}

Accessing sql server data into SparkCLR [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How data can be fetched from SQL Server in SparkCLR?
You could use the following SparkCLR code as a reference to use C# for loading Spark DataFrame from the data in SQL Server, Azure SQL Database or any other JDBC compliant datasource.
//C# sample to load SQL Server data as Spark DataFrame using JDBC
var sparkConf = new SparkConf();
var sparkContext = new SparkContext(sparkConf);
var sqlContext = new SqlContext(sparkContext);
var dataFrame = sqlContext.Read()
.Jdbc("jdbc:sqlserver://localhost:1433;databaseName=Temp;;integratedSecurity=true;", "xyz",
new Dictionary<string, string>());
dataFrame.ShowSchema();
var rowCount = dataFrame.Count();
Console.WriteLine("Row count is " + rowCount);
Few things to note:
This sample code uses Microsoft JDBC driver. If you use a different driver or JDBC datasource you need to update the url
You need to include the driver jar file when submitting your SparkCLR job
SparkCLR project for this sample is available # https://github.com/Microsoft/SparkCLR/tree/master/examples/JdbcDataFrame
My recommendation is to use JDBC to connect to sql server then query against the Dataframe.

Categories