.NET Core 6 SQL Server connection without Entity Framework - sql-server

I am new to .NET Core. I have defined the connection string in appsettings.json like this:
"ConnectionStrings": {
"TestBD": "Server=localhost;Database=Test;Trusted_Connection=True;MultipleActiveResultSets=true"
}
I am not using Entity Framework. I need to connect to the database using this connection string from the Program.cs file.
Any help is really appreciated. Thanks

You refer the following sample code to use ADO.NET in Asp.net 6 program.cs:
//required using Microsoft.Data.SqlClient;
app.MapGet("/movies", () =>
{
var movies = new List<Movie>();
//to get the connection string
var _config = app.Services.GetRequiredService<IConfiguration>();
var connectionstring = _config.GetConnectionString("DefaultConnection");
//build the sqlconnection and execute the sql command
using (SqlConnection conn = new SqlConnection(connectionstring))
{
conn.Open();
string commandtext = "select MovieId, Title, Genre from Movie";
SqlCommand cmd = new SqlCommand(commandtext, conn);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
var movie = new Movie()
{
MovieId = Convert.ToInt32(reader["MovieId"]),
Title = reader["Title"].ToString(),
Genre = reader["Genre"].ToString()
};
movies.Add(movie);
}
}
return movies;
});
The result like this:

Related

Npgsql 42601: date format not recognized

I am currently getting a date format not recognized error when trying to fetch from a refcursor. The query used in pgAdmin for both the function call and fetch match exactly with the call coming from the .NET code and yet only the .NET code is throwing this error. The result set only consists of character_varying and numeric. Currently using .NET 6 with Npgsql 6.0.3.
await using var connection = new NpgsqlConnection(connectionString);
await connection.OpenAsync();
var tran = await connection.BeginTransactionAsync();
await using (var command = new NpgsqlCommand(query, connection)
{
CommandType = System.Data.CommandType.Text,
Transaction = tran
})
{
command.Parameters.Add(new NpgsqlParameter("refCursor", NpgsqlTypes.NpgsqlDbType.Refcursor, 10, "refCursor",
System.Data.ParameterDirection.ReturnValue, false, 2, 2, System.Data.DataRowVersion.Current, null));
command.Prepare();
var cursorName = "";
await using (var portal = await command.ExecuteReaderAsync())
{
while (portal.Read())
{
cursorName = portal[0].ToString();
}
}
command.CommandText = $"fetch all in \"{cursorName}\"";
command.CommandType = System.Data.CommandType.Text;
await using (var reader = await command.ExecuteReaderAsync())
The exception points to: File: char_todatetime.c Routine: execute_char_todatetime_format. Any ideas why?

Angular 11 HTTP POST to SQL Server Database

I am trying to post my inputs to my SQL Server database. I can in fact POST to the database, but I get back a blank response. I know it's because I am returning "Success" instead of my variables but how to I correctly format that for the return statement?
POST method:
[HttpPost]
public JsonResult Post(Weather Wea)
{
string query = #"INSERT INTO dbo.Information (Date, TemperatureC, TemperatureF, Summary) VALUES ('" + Wea.Date + #"'
,'" + Wea.TemperatureC + #"'
,'" + Wea.TemperatureF + #"'
,'" + Wea.Summary + #"'
)";
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("WeatherAppCon");
SqlDataReader myReader;
using (SqlConnection myCon = new SqlConnection(sqlDataSource))
{
myCon.Open();
using (SqlCommand myCommand = new SqlCommand(query, myCon))
{
myReader = myCommand.ExecuteReader();
table.Load(myReader);
myReader.Close();
myCon.Close();
}
}
return new JsonResult("Success");
}
Front-end POST
export class PostDataComponent {
baseUrl: string;
date: number;
temperatureC: number;
summary: string;
weatherForm: FormGroup;
constructor(public http: HttpClient, #Inject('BASE_URL') baseUrl: string, private formBuilder: FormBuilder) {
this.baseUrl = "https://localhost:44347/WeatherForecast";
this.weatherForm = formBuilder.group({
Date: new FormControl(),
TemperatureC: new FormControl(),
Summary: new FormControl()
});
}
CreateData() {
const params = new HttpParams({
fromObject: {
'date': this.weatherForm.value.Date.toString(),
'temperatureC': this.weatherForm.value.TemperatureC.toString(),
'summary': this.weatherForm.value.Summary.toString()
}
});
console.log(params);
this.http.post(this.baseUrl, {},{ params: params }).subscribe(data => {
console.log(data);
});
}
}
Couple things here.
As marc_s commented, you should be using parameterization instead of concatenating to avoid any potential SQL injection:
string query = #"INSERT INTO dbo.Information (Date, TemperatureC, TemperatureF, Summary) VALUES (#Date, #TemperatureC, #TemperatureF, #Summary)";
...
using (System.Data.SqlClient.SqlCommand myCommand = new SqlCommand(query, myCon))
{
myCommand.Parameters.AddWithValue("#Date", Wea.Date);
myCommand.Parameters.AddWithValue("#TemperatureC", Wea.TemperatureC);
myCommand.Parameters.AddWithValue("#TemperatureF", Wea.TemperatureF);
myCommand.Parameters.AddWithValue("#Summary", Wea.Summary);
...
Unless you have a trigger on your target table with an output, your query isn't returning any data (just the number of rows inserted) and your SqlDataReader is empty. You could get rid of the reader/DataTable and use myCommand.ExecuteScalar() instead in this case. If you do have a trigger outputting the inserted data, disregard this.
If you don't have an output trigger but do still need to return the inserted values for whatever reason, you could keep your SqlDataReader and update your query to the following
string query = #"INSERT INTO dbo.Information (Date, TemperatureC, TemperatureF, Summary)
OUTPUT inserted.Date,inserted.TemperatureC,inserted.TemperatureF,inserted.Summary
VALUES (#Date, #TemperatureC, #TempreatureF, #Summary)";
Without knowing the response format you're looking for, it's hard to give an answer on how to generate it. If you need to return the inserted values, you could use the OUTPUT keyword as in the previous bullet and serialize your DataTable.

How to get names of all sheets in excel

I want to create a method to get names of all sheets in a workbook. My workbook has 7 sheets. If I want to read and save names of sheets to the variable excelSheets, I receive 9 names, where two names response to non-exists sheets ("lists$" and "TYPAB").
I don't understand where is the problem? How can I get names only the existing sheets?
public List<string> NamesOfSheets(string filename)
{
string con = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 12.0;HDR=Yes;'";
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
List<string> excelSheets;
try
{
DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
excelSheets = dt.Rows.Cast<DataRow>()
.Select(i => i["TABLE_NAME"].ToString()).ToList();
return excelSheets;
}
catch (Exception)
{
throw new Exception("Failed to get SheetName");
}
}
}
Oscar, thanks for your help, but office interlop doesn't solve my problem.
I found that "lists$" is hidden sheet, so only name TYPAB doesn't respond to any existing sheet.
So I added clause where and problem is solved. :)
public List<string> NamesOfSheets(string filename)
{
string con = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 12.0;HDR=Yes;'";
List<string> excelSheets;
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
try
{
DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
excelSheets = dt.Rows.Cast<DataRow>()
.Where(i => i["TABLE_NAME"].ToString().EndsWith("$") || i["TABLE_NAME"].ToString().EndsWith("$'"))
.Select(i => i["TABLE_NAME"].ToString()).ToList();
return excelSheets;
}
catch (Exception)
{
throw new Exception("Failed to get SheetName");
}
}
}
Why not use Office Interop for this?
foreach (Excel.Worksheet displayWorksheet in Globals.ThisWorkbook.Worksheets)
{
Debug.WriteLine(displayWorksheet.Name);
}
https://msdn.microsoft.com/en-us/library/59dhz064.aspx

SQL Server backup failed for server xxxx

I'm trying to take a backup of a local SQL Server database to my local machine through c# code and I am getting Backup failed for server .
Code as follows:
using (var con = new SqlConnection(conn))
{
const string destination = "d:\\backups"+"/";
var sqlServer = new Server(new ServerConnection(con));
con.Open();
var bkpDatabase = new Backup { Action = BackupActionType.Database, Database = name };
var bkpDevice = new BackupDeviceItem(destination + name + ".bak", DeviceType.File);
bkpDatabase.Devices.Add(bkpDevice);
bkpDatabase.Checksum = true;
bkpDatabase.ContinueAfterError = true;
bkpDatabase.Incremental = false;
bkpDatabase.Initialize = true;
// Perform the backup
bkpDatabase.SqlBackup(sqlServer);
con.Close();
}
I have used the following references :
mircrosoft.sqlserver.smo;
mircrosoft.sqlserver.smoextended;
mircrosoft.sqlserver.management.sdk.sfc;
mircrosoft.sqlserver.connectioninfo;
Inner exception :
Microsoft.SqlServer.Management.Smo.FailedOperationException was caught
HelpLink=http://go.microsoft.com/fwlink? ProdName=Microsoft+SQL+Server&ProdVer=10.50.2500.0+((KJ_PCU_Main).110617- 0026+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptio nText&EvtID=Backup+Server&LinkId=20476
HResult=-2146233088
Message=Backup failed for Server 'XXXXX'.
Source=Microsoft.SqlServer.SmoExtended
Operation=Backup
StackTrace:
at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)
at DatabaseCreation.Program.BackupDatabase(String name, String conn) in c:\Users\Documents\Visual Studio 2013\Projects\Databaseinstallation\DatabaseCreation\Program.cs:line 154
InnerException: System.NullReferenceException
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Microsoft.SqlServer.ConnectionInfo
StackTrace:
at Microsoft.SqlServer.Management.Common.ConnectionManager.get_IsOpen()
at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.PoolConnect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.get_DatabaseEngineType()
at Microsoft.SqlServer.Management.Smo.Server.GetExecutionManager()
at Microsoft.SqlServer.Management.Smo.Server.get_ExecutionManager()
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.get_ServerVersion()
at Microsoft.SqlServer.Management.Smo.Backup.Script(Server targetServer)
at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)
Maybe you have to escape the \ character in your destination path:
#"d:\backups"
or
"d:\\backups"
Try the following:
using (var con = new SqlConnection(conn))
{
const string destination = "c:\\backups";
var fileName = Path.Combine(destination, String.Format("{0}.bak", name));
if (!Directory.Exists(destination))
Directory.CreateDirectory(destination);
var sqlServer = new Server(new ServerConnection(con));
var bkpDatabase = new Backup
{
Action = BackupActionType.Database,
Database = name
};
var bkpDevice = new BackupDeviceItem(fileName, DeviceType.File);
bkpDatabase.Devices.Add(bkpDevice);
bkpDatabase.Checksum = true;
bkpDatabase.ContinueAfterError = true;
bkpDatabase.Incremental = false;
bkpDatabase.Initialize = true;
bkpDatabase.SqlBackup(sqlServer);
}

Changes to existing sqlite revert back to original

I,m working on an mobile flex application using flash Builder 4.6 and Flex 4.6, I have a SQlite db file in bin-debug folder that I copy to applicationStorageDirectory in ActionScript when the application starts.
Here is the script that setups the database it called on app initialize event:
/* Variables*/
private var file:File = File.applicationDirectory.resolvePath("db/myHealthBuddy.db");
private var local:File = File.applicationStorageDirectory.resolvePath("db/myHealthBuddy.db");
private var conn:SQLConnection;
private var model:Model = new Model();
/* db */
protected function dbInit():void
{
if(!local.exists)
{
local.createDirectory();
file.copyTo(local,true);
}
conn = new SQLConnection();
conn.addEventListener(SQLEvent.OPEN, openHandler);
conn.addEventListener(SQLErrorEvent.ERROR, errorHandler);
conn.openAsync(local);
}
private function openHandler(event:SQLEvent):void
{
trace("\ndb open");
conn.removeEventListener(SQLEvent.OPEN, openHandler);
// saving connection to Model valueObject
model.connection = conn;
// create tables if not already exists
var createTablesFile:File = File.applicationDirectory.resolvePath("db" + File.separator + "createtables.xml");
var stream:FileStream = new FileStream();
stream.open(createTablesFile, FileMode.READ);
var xml:XML = XML(stream.readUTFBytes(stream.bytesAvailable));
stream.close();
conn.begin(SQLTransactionLockType.IMMEDIATE);
for each (var statement:XML in xml.statement)
{
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = conn;
stmt.text = statement;
stmt.execute();
}
conn.commit();
navigator.firstViewData = model;
}
Is this code ok.
I need to make changes to the database e.g.(add new tables and add columns to existing tables). When I make these changes to the sqlite db they revert back but presist in the xml file which contains the tables creations SQL.

Resources