C# SQL查询是正确的,但未填充数据表
我试图用以下查询填充数据表:C# SQL查询是正确的,但未填充数据表,c#,sql,database,winforms,C#,Sql,Database,Winforms,我试图用以下查询填充数据表: SELECT recipes.Name, Instructions, recipes.Preperation_Time, Author FROM RecipeIngredients INNER JOIN recipes ON recipes.Recipe_ID = RecipeIngredients.Recipe_ID INNER JOIN Ingredients ON Ingredients.Ingredient_ID =
SELECT
recipes.Name, Instructions, recipes.Preperation_Time, Author
FROM
RecipeIngredients
INNER JOIN
recipes ON recipes.Recipe_ID = RecipeIngredients.Recipe_ID
INNER JOIN
Ingredients ON Ingredients.Ingredient_ID = RecipeIngredients.Ingredient_ID
WHERE
ingredients.Name IN ('eggs')
在.sql文件中运行此查询时,我会得到所需的结果:
然而,当我试图用查询填充数据表时,我没有得到任何结果。这不是我的数据库的问题,也不是我使用简单查询时的代码问题,例如:
"Select * FROM recipes"
我在datagridview中获取所有配方:
我是不是遗漏了什么
这是我的表单和数据库连接类的代码
private void resultsWindow_Load(object sender, EventArgs e)
{
// Format ingredient array for SQL Syntax
for(int i = 0; i< ingredientCount; i++)
{
string ing = ingredientArray[i];
string editedIng = "'" + ing + "'";
ingredientArray[i] = editedIng;
}
string ingredientString = string.Join(", ", ingredientArray);
// get connection string
string connectionString = Properties.Settings.Default.ConnectionString;
DataTable recipeDataTable = new DataTable();
conn = new DatabaseConnections(connectionString);
conn.openConnection();
// Get datatable
recipeDataTable = conn.getRecipes(ingredientString);
// Display data in grid view
recipesDataGrid.DataSource = recipeDataTable;
recipesDataGrid.Refresh();
}
public DataTable getRecipes(string ingredientString)
{
string sqlString = ("SELECT recipes.Name, Instructions, recipes.Preperation_Time, Author FROM RecipeIngredients" +
" INNER JOIN recipes ON recipes.Recipe_ID = RecipeIngredients.Recipe_ID" +
" INNER JOIN Ingredients ON Ingredients.Ingredient_ID = RecipeIngredients.Ingredient_ID" +
" WHERE ingredients.Name IN ('eggs')");
string sqlString_ = ("Select * FROM recipes");
DataTable recipeDataTable = new DataTable();
openConnection();
SqlCommand cmd = new SqlCommand(sqlString_, connectionToDB);
SqlDataAdapter da = new SqlDataAdapter(cmd);
// Fill dataset
da.Fill(recipeDataTable);
closeConnection();
return recipeDataTable;
}
private void resultsWindow\u加载(对象发送方,事件参数e)
{
//格式化SQL语法的成分数组
对于(int i=0;i
我没有尝试您的代码,但您可能只是在表中添加了数据,尚未验证。
能否在T-SQL编辑器中尝试以下语句:
提交事务
看起来应该能用。我怀疑是使用了错误的服务器,还是默认表错误。您可以向查询中添加“UseDatabase”以查看是否使用了正确的表。很可能您有多个mdf文件。连接字符串不应包含mdf文件。只需为连接字符串中的名称指定服务器名称和实例。没有附加语句。数据库已经连接。@jdweng我尝试使用数据库,但对数据库没有影响outcome@goldenpeng语法看起来不错。你能从配料中选择*以查看1)它是否包含数据,2)它是否拼写为“鸡蛋”@JimCastro是的,这很有帮助,我没有把鸡蛋里的E资本化。。。我的查询现在起作用了。现在我正试图停止从查询中获取重复的结果。谢谢。@jdwengt使用数据库确实会产生影响,所以请忽略我的最后一条评论。我的查询现在可以运行了,但现在我担心的是,如果这个程序被发送给其他人,这个字符串将无法运行,因为它是我的计算机所独有的,有什么方法可以这样做,以便它可以跨系统运行吗?