I am trying to establish SQL connection but fails
Error is somehow related to globalization.
9 Replies
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}");
}
}
}
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
Try putting this in your csproj file:
<PropertyGroup>
<InvariantGlobalization>false</InvariantGlobalization>
</PropertyGroup>
<PropertyGroup>
<InvariantGlobalization>false</InvariantGlobalization>
</PropertyGroup>
isn't csproj like the whole project?
with other files?
like when i tried to open it, it opende the whole thing
Open it in a text editor
Paste in the above.
Save it.
You can also do this in Visual Studio:
(Right click project, choose Edit Project File)
If you want to use VS as your text editor.
like this?
yeah
thanks
Yeah, change that Invariant thing to false.
sure
thank you so much
no i finnally have a different error