使用ASP.NET插入数据库会导致错误

时间:2021-05-06 15:41:46

I am working with VS 2017 for the first time.

我是第一次与VS 2017合作。

I am trying to insert some values into a database. I already made the connection (checked with select statement) with the database, but now I am stuck in the insert portion.

我试图将一些值插入数据库。我已经使用数据库建立了连接(使用select语句检查),但现在我陷入了插入部分。

I have a SqlDataSource defined as:

我有一个SqlDataSource定义为:

<div>
   <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="  <%$ ConnectionStrings:Sphinxx_Conn %>" 
        ProviderName="<%$ ConnectionStrings:Sphinxx_Conn.ProviderName %>"  
        InsertCommand="INSERT INTO &quot;DEMO&quot; (&quot;ID&quot;, &quot;NAME&quot;) VALUES (:ID, :NAME)">
        <InsertParameters>
            <asp:Parameter Name="ID" Type="String" />
            <asp:Parameter Name="NAME" Type="String" />
        </InsertParameters>
   </asp:SqlDataSource>
</div>

Now the following snippet:

现在,以下代码段:

 SqlDataSource1.InsertParameters["ID"].DefaultValue = '1'
 SqlDataSource1.InsertParameters["NAME"].DefaultValue = 'John'

Underlines the 'SqlDataSource1.InsertParameters' part and shows an error:

在“SqlDataSource1.InsertParameters”部分下划线并显示错误:

Property access must assign to the property or use its value.

属性访问必须分配给属性或使用其值。

What exactly am I doing wrong?

我究竟做错了什么?

2 个解决方案

#1


2  

You are passing invalid data because you are using single quotes, use double quotes instead:

您传递的是无效数据,因为您使用的是单引号,而是使用双引号:

 SqlDataSource1.InsertParameters["ID"].DefaultValue = "1";
 SqlDataSource1.InsertParameters["NAME"].DefaultValue = "John";

On the side note, if you started working and getting to know web development with Visual Studio, i recommend you to look at MVC. Web Forms are old tech that is no longer supported.

另外,如果您开始使用Visual Studio开始工作并了解Web开发,我建议您查看MVC。 Web窗体是不再受支持的旧技术。

#2


0  

There are 2 errors within your Code Snippet of assigning values.

您的代码段中有2个错误,分配值。

  1. You are setting a char value instead of string, replace single quoted 'values with double quotes ". Instead of '1' do this "1" same with John too.
  2. 你正在设置一个char值而不是字符串,用双引号替换单引号'值。而不是'1'这个“1”也与John相同。

  3. You have not used ; to terminate your statement, so put semicolons in the end of your statements.
  4. 你还没用过;终止你的陈述,所以在你的陈述结尾加上分号。

#1


2  

You are passing invalid data because you are using single quotes, use double quotes instead:

您传递的是无效数据,因为您使用的是单引号,而是使用双引号:

 SqlDataSource1.InsertParameters["ID"].DefaultValue = "1";
 SqlDataSource1.InsertParameters["NAME"].DefaultValue = "John";

On the side note, if you started working and getting to know web development with Visual Studio, i recommend you to look at MVC. Web Forms are old tech that is no longer supported.

另外,如果您开始使用Visual Studio开始工作并了解Web开发,我建议您查看MVC。 Web窗体是不再受支持的旧技术。

#2


0  

There are 2 errors within your Code Snippet of assigning values.

您的代码段中有2个错误,分配值。

  1. You are setting a char value instead of string, replace single quoted 'values with double quotes ". Instead of '1' do this "1" same with John too.
  2. 你正在设置一个char值而不是字符串,用双引号替换单引号'值。而不是'1'这个“1”也与John相同。

  3. You have not used ; to terminate your statement, so put semicolons in the end of your statements.
  4. 你还没用过;终止你的陈述,所以在你的陈述结尾加上分号。