CREATE PROCEDURE [dbo].[GetToursList](
@toCity nvarchar(30)
, @toCountry nvarchar(30)
, @departureDate date
, @amountNights int)
AS
BEGIN
SET @toCity = N'Сиде'
SET @toCountry = N'Турция'
SELECT Hotels.[Name] AS HotelName,
Cities.[Name] AS CityName,
Countrys.[Name] AS CounrtyName,
(SELECT MIN(Price)
FROM HotelsRooms
WHERE (HotelID = Hotels.ID)) AS MinPrice,
Hotels.Rating AS Stars,
Hotels.Line,
Hotels.DistanceToBeach,
Hotels.DistanceToAirport
FROM Hotels
INNER JOIN Cities ON Hotels.City = Cities.ID
INNER JOIN Countrys ON Cities.CountryID = Countrys.ID
WHERE (NOT (@toCity IS NULL) OR Cities.[Name] = @toCity)
AND (NOT (@toCountry IS NULL) OR Countrys.[Name] = @toCountry)
END
исправьте ошибкукакую? rocedure contains an unresolved reference to an object? Я её исправил. Указал как вы и сказали из какой таблицы брать поле. Ошибка пропала и процедура обновилась.
не совсем понимаю о чем речь.
нужны тестовые данные
DECLARE @toCity NVARCHAR(30)
SET @toCity = N'Сиде'
DECLARE @toCountry NVARCHAR(30)
SET @toCountry = N'Турция'
SELECT
hotels.Name as HotelName
,cities.Name as CityName
,countrys.Name as CounrtyName
,hotels.Rating as Stars
,hotels.Line as Line
,hotels.DistanceToAirport as DistToAirport
,hotels.DistanceToBeach as DistTobeach
FROM dbo.Tours
INNER JOIN dbo.Countrys as countrys
ON Tours.CountryID = countrys.Id
INNER JOIN dbo.Cities as cities
ON Tours.CityID = cities.Id
INNER JOIN dbo.Hotels as hotels
ON Tours.HotelID = hotels.Id
WHERE
/*(@toCity is null or*/(Tours.CityID = (select cities.Id from cities where cities.Name = @toCity))--)
or
/*(@toCountry is null or*/(Tours.CountryID = (select countrys.Id from countrys where countrys.Name = @toCountry))--)
SELECT
hotels.Name as HotelName
,cities.Name as CityName
,countrys.Name as CounrtyName
,hotels.Rating as Stars
,hotels.Line as Line
,hotels.DistanceToAirport as DistToAirport
,hotels.DistanceToBeach as DistTobeach
FROM dbo.Tours
INNER JOIN dbo.Countrys as countrys
ON Tours.CountryID = countrys.Id
INNER JOIN dbo.Cities as cities
ON Tours.CityID = cities.Id
INNER JOIN dbo.Hotels as hotels
ON Tours.HotelID = hotels.Id
WHERE
(@toCity is null or(Tours.CityID = (select cities.Id from cities where cities.Name = @toCity)))
or
(@toCountry is null or(Tours.CountryID = (select countrys.Id from countrys where countrys.Name = @toCountry)))
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:52822",
"sslPort": 44314
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Booking": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
declare @cityName nvarchar(30)
SET @cityName = N'Сиде'
DECLARE @CityID int
set @CityID = (SELECT Cities.Id FROM dbo.Cities WHERE Cities.Name = @cityName)
select @CityID
declare @cityName nvarchar(30)
SET @cityName = 'Сиде'
DECLARE @CityID int
set @CityID = (SELECT Cities.Id FROM dbo.Cities WHERE Cities.Name = @cityName)
select @CityID
set @CityID = (SELECT Cities.Id FROM dbo.Cities WHERE Cities.Name = N(@cityName))
'N' is not a recognized built-in function name., и с обновлением тоже самое. Поставил точку остановки на вызове процедуры из C# - все правильно передает.
public static void AddHotel(string cityName, string adress, int rating, int? line, string name, int? distanceToBeach, int? @distaceToAirport)
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
string sql = "[dbo].[AddHotel]";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@adress", SqlDbType.NVarChar, 60);
command.Parameters["@adress"].Value = adress;
command.Parameters.Add("@rating", SqlDbType.Int);
command.Parameters["@rating"].Value = rating;
command.Parameters.Add("@line", SqlDbType.Int);
command.Parameters["@line"].Value = rating;
command.Parameters.Add("@name", SqlDbType.NVarChar, 60);
command.Parameters["@name"].Value = name;
command.Parameters.Add("@distanceToBeach", SqlDbType.Int);
command.Parameters["@distanceToBeach"].Value = distanceToBeach;
command.Parameters.Add("@distanceToAirport", SqlDbType.Int);
command.Parameters["@distanceToAirport"].Value = distaceToAirport;
command.Parameters.Add("@cityName", SqlDbType.NVarChar, 30);
command.Parameters["@cityName"].Value = cityName;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
В INSERT [Hotels] нет поля кода города.: забыл добавить:
INSERT INTO [dbo].[Hotels]
([Name], [City],[Address],[Rating], [DistanceToBeach], [DistanceToAirport], [Line], [Discription])
VALUES
(@name,@CityID,@adress, @rating, @distanceToBeach, @distanceToAirport, @line, 'some discr')
Дополнение. Поскольку вы передаёте город не кодом, а названием, нужно обрабатывать его отсутствие в базе:
{ { "Турция", "Город в турции" }, { "Турция", "Город в турции2" }, { "Греция", "Город в Греции" } };
{ { "Турция", "Город в турции" }, { "Турция", "Город в турции2" }, { "Греция", "Город в Греции" } };
, чтобы потом отправить в этот метод:public static void AddCity(string cityName, string countryName)
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
string sql = "[dbo].[AddCity]";
using (SqlCommand command = new SqlCommand(sql, connection))
{
DataTable dataTable = new DataTable();
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@param", SqlDbType.NVarChar, 30);
command.Parameters["@param"].Value = cityName;
command.Parameters.Add("@param2", SqlDbType.NVarChar, 30);
command.Parameters["@param2"].Value = countryName;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
}
, дальше в эту процедуру:CREATE PROCEDURE [dbo].[AddCity]
@param nvarchar (MAX),
@param2 nvarchar (MAX)
AS
BEGIN
DECLARE @CountryID NVARCHAR(MAX)
SET @CountryID = (SELECT Countrys.Id from dbo.Countrys where Countrys.Name = @param2)
INSERT INTO [dbo].[Cities]
([Name],[CountryID])
VALUES
(@param, @CountryID)
END
string[,] cities = new string[,] { };
for (int i = 0; i < cities.Length; i++)
{
Querys.AddCity(cities[i]);//так не делается походу
}
public static void AddCity(string cityName, string countryName)
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
string sql = "[dbo].[AddCity]";
using (SqlCommand command = new SqlCommand(sql, connection))
{
DataTable dataTable = new DataTable();
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@param", SqlDbType.NVarChar, 30);
command.Parameters["@param"].Value = cityName;
command.Parameters.Add("@param2", SqlDbType.NVarChar, 30);
command.Parameters["@param2"].Value = countryName;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
}
CREATE PROCEDURE [dbo].[AddCity]
@param nvarchar (MAX),
@param2 nvarchar (MAX)
AS
BEGIN
DECLARE @CountryID NVARCHAR(MAX)
SET @CountryID = (SELECT Countrys.Id from dbo.Countrys where Countrys.Name = @param2)
INSERT INTO [dbo].[Cities]
([Name],[CountryID])
VALUES
(@param, @CountryID)
END
IEnumerator GenerateString()
{
mainText.text = "";
for (int i = 0; i < needText.Length; i++)
{
mainText.text += needText[i];
mainText.text += GenerateRandom(needText.Length - i);
//mainText.text += GenerateRandom(needText.Length-i);
yield return new WaitForSeconds(speedChanging);
mainText.text = mainText.text.Substring(0, i + 1);
}
}
IEnumerator GenerateString()
{
mainText.text = "";
for (int i = 0; i < needText.Length; i++)
{
mainText.text += needText[i];
mainText.text = mainText.text.Substring(i, mainText.text.Length);
mainText.text += GenerateRandom(needText.Length - i);
//mainText.text += GenerateRandom(needText.Length-i);
yield return new WaitForSeconds(speedChanging);
}
}
ArgumentOutOfRangeException: Index and length must refer to a location within the string.. Не понимаю, что не так:(
Parameter name: length
ответы от базы данных я сереализую через JsonConvert(от Newtonsoft) и отправлю клиенту ответ таким образом:
Quuerys.GetTours возвращает объект типа
DataTable
.Мне нужен запрос который будет выводить данные об отеле, в том числе о комнатах в нем.
В json это должно выглядеть так:
Я не представляю в dateTable вместить данные об комнате, чтобы сами данные отеля не повторялись с каждой запись о комнате. Нет идей даже как гуглить такое. С dataTable такое проделать наверное невозможно?