我想自动增加并在cloumn“RefernceNo”中插入值。请帮忙!! [英] I want to auto increment and insert values in a cloumn "RefernceNo". Please HELP!!

查看:102
本文介绍了我想自动增加并在cloumn“RefernceNo”中插入值。请帮忙!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML: -



 <      class   =  abtus >  
< tr >
< td class = style2 >
< asp:Label ID = Label1 runat = server Text = 标签 > < / asp:标签 >
< / td >
< td class = style1 >
< asp:FileUpload ID = FileUpload1 runat = server / >
< / td >
< td >
< asp:TextBox ID = TextBox2 runat = server CssClass = txtbx > < / asp:TextBox >
< / td > ;
< / tr >
< tr >
< < span class =code-leadattribute> td class = style2 >
< asp:标签 ID = Label2 runat = server 文字 = 标签 > < / asp:Label >
< / td >
< td class = style1 >
< asp:FileUpload ID = FileUpload2 runat = server / >
< / td >
< td >
< asp:TextBox ID = TextBox3 runat = server CssClass = txtbx > < / asp:TextBox >
< / td >
< / tr >
< tr >
< td class = style2 >
< asp:标签 ID = Label3 runat = server 文字 = 标签 > < / asp:标签 >
< / td >
< td class = style1 >
< asp:FileUpload ID = FileUpload3 runat = server / >
< / td >
< td >
< asp:TextBox ID = TextBox4 runat = 服务器 CssClass = txtbx > < / asp:TextBox >
< / td >
< / tr >
< tr >
< td class = style2 >
< asp:标签 ID = Label4 runat < span class =code-keyword> = server 文本 = 标签 > < / asp:Label >
< / td >
< td class = style1 >
< asp:FileUpload ID = FileUpload4 < span class =code-attribute> runat = server / >
< / td >
< td >
< asp :TextBox ID = TextBox5 runat = server CssClass = txtbx > ; < / asp:TextBox > ;
< / td >
< / tr >
< tr >
< td class = style2 >
< span class =code-keyword><
asp:Label ID = Label5 runat = server 文本 = 标签 > < / asp:Label >
< / td >
< td class = style1 >
< asp:FileUpload ID = FileUpload5 runat = server / >
< / td >
< td >
< asp:TextBox ID = TextBox6 < span class =code-attribute> runat = server CssClass = txtbx > < / asp:TextBox >
< / td >
< / tr < span class =code-keyword>>
< tr >
< td class = style2 >
< asp:Label ID = Label6 runat = server 文字 = 标签 > < / asp:标签 >
< / td >
< td class = style1 >
< asp:FileUpload ID = FileUpload6 runat = server / >
< / td >
< td >
< asp:TextBox ID = TextBox7 runat = 服务器 CssClass = txtbx > < < span class =code-leadattribute> / asp:TextBox
>
< / td >
< / tr >
< / table >













C#代码: -

  protected   void  Button1_Click( object  sender,EventArgs e)
{

if (FileUpload1.HasFile)
{
uploadimage(Label1,FileUpload1,TextBox2);
}

if (FileUpload2.HasFile)
{
uploadimage(Label2,FileUpload2,TextBox3) ;
}

if (FileUpload3.HasFile)
{
uploadimage(Label3,FileUpload3,TextBox4) ;
}

if (FileUpload4.HasFile)
{
uploadimage(Label4,FileUpload4,TextBox5) ;
}

if (FileUpload5.HasFile)
{
uploadimage(Label5,FileUpload5,TextBox6) ;
}

if (FileUpload6.HasFile)
{
uploadimage(Label6,FileUpload6,TextBox7) ;
}

ScriptManager.RegisterClientScriptBlock( this this .GetType (), alertMessage alert('图片上传成功') true );
}

public void uploadimage(标签l1,FileUpload f1 ,TextBox t1)
{
string str = @ data source = DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog = new; integrated security = true;
SqlConnection con = new SqlConnection(str);
con.Open();
string s = Server.MapPath( Img /)+ f1.FileName;
f1.SaveAs(s);





这里我要添加一个列名为RefrenceNo的新记录: -



  SqlDataAdapter da =  new  SqlDataAdapter( 从日期1中选择计数(*)+ 1作为d ,con);  

string query = 插入date1值(' + Label1.Text + ',' + TextBox1.Text + ',' + f1.FileName + ',' + t1.Text + ');
SqlCommand cmd = new SqlCommand(query,con);
cmd.ExecuteNonQuery();
con.Close();

}

解决方案

由于没有,自己查询和增加数字的想法不好保证唯一性,使用SQL服务器 IDENTITY 这就是它的用途: http://www.w3schools.com/sql/sql_autoincrement.asp [ ^ ]


create table date1(RefrenceNo int identity(1,1),ddate datetime,image image,detail varchar(20))

使用此查询创建表格



并使用以下查询将数据插入表格

bt不要在RefrenceNo中插入数据此列的值将自动生成

插入date1值('+ TextBox1.Text +','+ f1.FileName +','+ t1.Text +' );

My HTML:-

<table class="abtus">
              <tr>
                  <td class="style2">
                      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                  </td>
                  <td class="style1">
                      <asp:FileUpload ID="FileUpload1" runat="server" />
                  </td>
                  <td>
                      <asp:TextBox ID="TextBox2" runat="server" CssClass="txtbx"></asp:TextBox>
                  </td>
              </tr>
              <tr>
                  <td class="style2">
                      <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                  </td>
                  <td class="style1">
                      <asp:FileUpload ID="FileUpload2" runat="server" />
                  </td>
                  <td>
                      <asp:TextBox ID="TextBox3" runat="server" CssClass="txtbx"></asp:TextBox>
                  </td>
              </tr>
              <tr>
                  <td class="style2">
                      <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
                  </td>
                  <td class="style1">
                      <asp:FileUpload ID="FileUpload3" runat="server" />
                  </td>
                  <td>
                      <asp:TextBox ID="TextBox4" runat="server" CssClass="txtbx"></asp:TextBox>
                  </td>
              </tr>
              <tr>
                  <td class="style2">
                      <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
                  </td>
                  <td class="style1">
                      <asp:FileUpload ID="FileUpload4" runat="server" />
                  </td>
                  <td>
                      <asp:TextBox ID="TextBox5" runat="server" CssClass="txtbx"></asp:TextBox>
                  </td>
              </tr>
              <tr>
                  <td class="style2">
                      <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
                  </td>
                  <td class="style1">
                      <asp:FileUpload ID="FileUpload5" runat="server" />
                  </td>
                  <td>
                      <asp:TextBox ID="TextBox6" runat="server" CssClass="txtbx"></asp:TextBox>
                  </td>
              </tr>
              <tr>
                  <td class="style2">
                      <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>
                  </td>
                  <td class="style1">
                      <asp:FileUpload ID="FileUpload6" runat="server" />
                  </td>
                  <td>
                      <asp:TextBox ID="TextBox7" runat="server" CssClass="txtbx"></asp:TextBox>
                  </td>
              </tr>
          </table>







C# Code:-

protected void Button1_Click(object sender, EventArgs e)
    {
        
        if (FileUpload1.HasFile)
        {
            uploadimage(Label1,FileUpload1,TextBox2);
        }

        if (FileUpload2.HasFile)
        {
            uploadimage(Label2, FileUpload2,TextBox3);
        }

        if (FileUpload3.HasFile)
        {
            uploadimage(Label3, FileUpload3,TextBox4);
        }

        if (FileUpload4.HasFile)
        {
            uploadimage(Label4, FileUpload4,TextBox5);
        }

        if (FileUpload5.HasFile)
        {
            uploadimage(Label5, FileUpload5,TextBox6);
        }

        if (FileUpload6.HasFile)
        {
            uploadimage(Label6, FileUpload6,TextBox7);
        }

        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Pictures Uploaded Successfully')", true);
    }

    public void uploadimage(Label l1, FileUpload f1, TextBox t1)
    {
        string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS;initial catalog=new; integrated security=true";
        SqlConnection con = new SqlConnection(str);
        con.Open();
        string s = Server.MapPath("Img/") + f1.FileName;
        f1.SaveAs(s);



Here I want to add a new record with column name as RefrenceNo:-

    SqlDataAdapter da = new SqlDataAdapter("Select count(*)+1 from as d from date1", con);

    string query = "insert into date1 values('"+Label1.Text+"','" + TextBox1.Text + "','" + f1.FileName + "','" + t1.Text + "')";
    SqlCommand cmd = new SqlCommand(query, con);
    cmd.ExecuteNonQuery();
    con.Close();

}

解决方案

Bad idea to query and increment a number yourself since there is no guarantee of uniqueness, use the SQL server IDENTITY that's what it is there for : http://www.w3schools.com/sql/sql_autoincrement.asp[^]


create table date1(RefrenceNo int identity(1,1),ddate datetime,image image,detail varchar(20))
use this query for creation of table

and use following query for inserting data to table
bt dont insert data in RefrenceNo value for this column will be automatically generated
insert into date1 values('" + TextBox1.Text + "','" + f1.FileName + "','" + t1.Text + "')";


这篇关于我想自动增加并在cloumn“RefernceNo”中插入值。请帮忙!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆