C
C#7mo ago
Dachi

I am trying to establish SQL connection but fails

Error is somehow related to globalization.
9 Replies
Dachi
Dachi7mo ago
c#
using BookLibraryAPI.Models;
using Microsoft.Data.SqlClient;

namespace BookLibraryAPI.Data;

public class BookShelfDB
{
public static HashSet<Shelf> Shelf = new();
private const string ConnectionString = "Data Source=localhost;Initial Catalog=BookShelf;User ID=sa;Password=Dachi123;";

public BookShelfDB()
{
using var connection = new SqlConnection(ConnectionString);
connection.Open();


var createShelfTableCommand = @"
CREATE TABLE Shelf (
Id INT PRIMARY KEY,
Name NVARCHAR(255),
BookId INT FOREIGN KEY REFERENCES Book(Id)
)";

ExecuteSqlCommand(connection, createShelfTableCommand, "Shelf");

// Step 3: Execute SQL commands to create the Book table
var createBookTableCommand = @"
CREATE TABLE Book (
Id INT PRIMARY KEY,
ShelfId INT FOREIGN KEY REFERENCES Shelf(Id),
Title NVARCHAR(255),
ISBN NVARCHAR(20),
Description NVARCHAR(MAX)
)";

ExecuteSqlCommand(connection, createBookTableCommand, "Book");
connection.Close();
}
private static void ExecuteSqlCommand(SqlConnection connection, string commandText, string tableName)
{
using var command = new SqlCommand(commandText, connection);
try
{
command.ExecuteNonQuery();
Console.WriteLine($"Table '{tableName}' created successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Error creating table '{tableName}': {ex.Message}");
}
}

}
c#
using BookLibraryAPI.Models;
using Microsoft.Data.SqlClient;

namespace BookLibraryAPI.Data;

public class BookShelfDB
{
public static HashSet<Shelf> Shelf = new();
private const string ConnectionString = "Data Source=localhost;Initial Catalog=BookShelf;User ID=sa;Password=Dachi123;";

public BookShelfDB()
{
using var connection = new SqlConnection(ConnectionString);
connection.Open();


var createShelfTableCommand = @"
CREATE TABLE Shelf (
Id INT PRIMARY KEY,
Name NVARCHAR(255),
BookId INT FOREIGN KEY REFERENCES Book(Id)
)";

ExecuteSqlCommand(connection, createShelfTableCommand, "Shelf");

// Step 3: Execute SQL commands to create the Book table
var createBookTableCommand = @"
CREATE TABLE Book (
Id INT PRIMARY KEY,
ShelfId INT FOREIGN KEY REFERENCES Shelf(Id),
Title NVARCHAR(255),
ISBN NVARCHAR(20),
Description NVARCHAR(MAX)
)";

ExecuteSqlCommand(connection, createBookTableCommand, "Book");
connection.Close();
}
private static void ExecuteSqlCommand(SqlConnection connection, string commandText, string tableName)
{
using var command = new SqlCommand(commandText, connection);
try
{
command.ExecuteNonQuery();
Console.WriteLine($"Table '{tableName}' created successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Error creating table '{tableName}': {ex.Message}");
}
}

}
error:
c#
Unhandled exception. System.Globalization.CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name')
en-us is an invalid culture identifier.
at System.Globalization.CultureInfo.GetCultureInfo(String name)
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
at Microsoft.Data.SqlClient.SqlConnection.Open()
at BookLibraryAPI.Data.BookShelfDB..ctor() in /Users/dachilekborashvili/Documents/CredoAssignments/BookLibraryAPI/Data/BookShelfDB.cs:line 14
at Program.<Main>$(String[] args) in /Users/dachilekborashvili/Documents/CredoAssignments/BookLibraryAPI/Program.cs:line 19
c#
Unhandled exception. System.Globalization.CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name')
en-us is an invalid culture identifier.
at System.Globalization.CultureInfo.GetCultureInfo(String name)
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
at Microsoft.Data.SqlClient.SqlConnection.Open()
at BookLibraryAPI.Data.BookShelfDB..ctor() in /Users/dachilekborashvili/Documents/CredoAssignments/BookLibraryAPI/Data/BookShelfDB.cs:line 14
at Program.<Main>$(String[] args) in /Users/dachilekborashvili/Documents/CredoAssignments/BookLibraryAPI/Program.cs:line 19
error happend when trying to performe conneciton.Open()
mtreit
mtreit7mo ago
Try putting this in your csproj file:
<PropertyGroup>
<InvariantGlobalization>false</InvariantGlobalization>
</PropertyGroup>
<PropertyGroup>
<InvariantGlobalization>false</InvariantGlobalization>
</PropertyGroup>
Dachi
Dachi7mo ago
isn't csproj like the whole project? with other files? like when i tried to open it, it opende the whole thing
mtreit
mtreit7mo ago
Open it in a text editor Paste in the above. Save it.
mtreit
mtreit7mo ago
You can also do this in Visual Studio:
No description
mtreit
mtreit7mo ago
(Right click project, choose Edit Project File) If you want to use VS as your text editor.
Dachi
Dachi7mo ago
like this? yeah thanks
mtreit
mtreit7mo ago
Yeah, change that Invariant thing to false.
Dachi
Dachi7mo ago
sure thank you so much no i finnally have a different error
Want results from more Discord servers?
Add your server
More Posts