I have created a local database in Visual Studio 2010 and a web application which interacts with it. I want to create a search function in order to obtain some rows from a table. I have a text field where i write a date (delivery date for a product, for example) and a button. On clicking the button, i want to bring on my web page information about an invoice. Please, check out my code:
我在Visual Studio 2010中创建了一个本地数据库和一个与之交互的web应用程序。我想创建一个搜索函数,以便从表中获取一些行。我有一个文本字段,我在其中编写一个日期(例如,产品的交付日期)和一个按钮。点击按钮时,我想打开关于发票的网页信息。请检查我的代码:
SqlConnection con = new SqlConnection(@"Data Source=JOHN-PC\SQLEXPRESS;Initial Catalog=subiect1;Integrated Security=True");
try
{
con.Open();
DataSet datas = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter("select * from Factura where Data_Livrare = @Data_Livrare", con);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);
commandBuilder.Parameters.Add(new SqlParameter("@Data_Livrare", (object)txtData.Text));
SqlDataReader reader = reader.ExecuteReader();
while (reader.Read())
{
adapter.Fill(datas);
}
}
catch (Exception ex)
{
Label3.Text = "Error --> " + ex.Message;
}
finally
{
con.Close();
}
I get the error:
我得到了错误:
"A using namespace directive can only be applied to namespaces; System.Data.SqlClient.SqlCommand is a type, not a namespace".
If i delete the System.Data.SqlClient.SqlCommand directive, i get plenty of errors referring to some methods which are, apparently, undefined (such as Parameters from commandBuilder.Parameters.Add
and ExecuteReader()
). When i run the program in browser, complete the textfield and click the button, i get:
如果我删除了System.Data.SqlClient。SqlCommand指令,我在引用一些显然没有定义的方法(比如commandbuild .Parameters)时得到了大量错误。添加和ExecuteReader())。当我在浏览器中运行程序,完成textfield,点击按钮,我得到:
" Fill: SelectCommand.Connection property has not been initialized."
What am i missing here? Is it only the user directive wrong or i simply don't approach well this problem?
我错过了什么?是用户指令错了,还是我没有很好地解决这个问题?
I must mention I am a beginner in C#.
我必须提一下,我是c#的初学者。
1 个解决方案
#1
2
Check your using
clauses at the top of your code.
在你的代码顶部检查你的使用条款。
I think you have using System.Data.SqlClient.SqlCommand;
我认为你已经使用System.Data.SqlClient.SqlCommand;
You should have System.Data.SqlClient;
你应该System.Data.SqlClient;
If you check the MSDN docs you will see that the namespace for System.Data.SqlClient.SqlCommand
is System.Data.SqlClient
如果您检查MSDN文档,您将看到System.Data.SqlClient的名称空间。SqlCommand是System.Data.SqlClient
#1
2
Check your using
clauses at the top of your code.
在你的代码顶部检查你的使用条款。
I think you have using System.Data.SqlClient.SqlCommand;
我认为你已经使用System.Data.SqlClient.SqlCommand;
You should have System.Data.SqlClient;
你应该System.Data.SqlClient;
If you check the MSDN docs you will see that the namespace for System.Data.SqlClient.SqlCommand
is System.Data.SqlClient
如果您检查MSDN文档,您将看到System.Data.SqlClient的名称空间。SqlCommand是System.Data.SqlClient