我不知道怎么写
c#
密码我正试图在这里运行一个示例代码-
https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/quickstart-dotnet?tabs=azure-portal%2Cwindows%2Cpasswordless%2Csign-in-azure-cli#create-account
这是我复制的代码
powershell
。当我这样做的时候
dotnet run
,我出错了
/home/manu/Program.cs(24,1): error CS8803: Top-level statements must precede namespace and type declarations. [/home/manu/manu.csproj]
The build failed. Fix the build errors and run again.
密码
// See https://aka.ms/new-console-template for more information
using Microsoft.Azure.Cosmos;
using Azure.Identity;
// New instance of CosmosClient class
using CosmosClient client = new(
accountEndpoint: Environment.GetEnvironmentVariable("COSMOS_ENDPOINT"),
tokenCredential: new DefaultAzureCredential()
);
// C# record representing an item in the container
public record Product(
string id,
string categoryId,
string categoryName,
string name,
int quantity,
bool sale
);
// Database reference with creation if it does not already exist
Database database = client.GetDatabase(id: "cosmicworks");
Console.WriteLine("Hello, World!");
Console.WriteLine($"New database:\t{database.Id}");
// Container reference with creation if it does not alredy exist
Container container = database.GetContainer(id: "products");
Console.WriteLine($"New container:\t{container.Id}");
// Create new object and upsert (create or replace) to container
Product newItem = new(
id: "70b63682-b93a-4c77-aad2-65501347265f",
categoryId: "61dba35b-4f02-45c5-b648-c6badc0cbd79",
categoryName: "gear-surf-surfboards",
name: "Yamba Surfboard",
quantity: 12,
sale: false
);
Product createdItem = await container.CreateItemAsync<Product>(
item: newItem
);
Console.WriteLine($"Created item:\t{createdItem.id}\t[{createdItem.categoryName}]");
// Point read item from container using the id and partitionKey
Product readItem = await container.ReadItemAsync<Product>(
id: "70b63682-b93a-4c77-aad2-65501347265f",
partitionKey: new PartitionKey("61dba35b-4f02-45c5-b648-c6badc0cbd79")
);
// Create query using a SQL string and parameters
var query = new QueryDefinition(
query: "SELECT * FROM products p WHERE p.categoryId = @categoryId"
)
.WithParameter("@categoryId", "61dba35b-4f02-45c5-b648-c6badc0cbd79");
using FeedIterator<Product> feed = container.GetItemQueryIterator<Product>(
queryDefinition: query
);
while (feed.HasMoreResults)
{
FeedResponse<Product> response = await feed.ReadNextAsync();
foreach (Product item in response)
{
Console.WriteLine($"Found item:\t{item.name}");
}
}
构建代码的正确方法是什么?