select和insert语句C#asp.net中的问题 [英] problem in select and insert statement C# asp.net

查看:125
本文介绍了select和insert语句C#asp.net中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是asp.net的新手,我正在为我的学者做这个项目。请帮助



PS:目前我收到错误,你必须在固定或使用声明声明中提供初始化程序



 public partial class _Default:System.Web.UI.Page 
{
SqlConnection con = new SqlConnection( 数据源= JIMMY-PC;初始目录= jimmy;集成安全性=真);
protected void Page_Load(object sender,EventArgs e)
{

}
protected void Button2_Click(object sender,EventArgs e)
{
Response.Redirect(home.aspx);
}
protected void Button1_Click(object sender,EventArgs e)
{
using(SqlCommand cnd = new SqlCommand(从Emplo中选择LAST EmpID),con)//错误这里
{
//如何存储选定的字段?
}
使用(SqlCommand cmd = new SqlCommand(Insert into Emplo([Name],[Password],[Designation],[Dept],[DOB],[Sex],[Address])值(@ Name,@ pass,@ des,@ dept,@ DOB,@ Sex,@ Address),con))
{
cmd.Parameters.Add(@ Name,SqlDbType。 VarChar).Value = TextBox1.Text;
//这里我需要添加前一个语句中的选定数字,并在此处插入当前行
cmd.Parameters.Add(@ des,SqlDbType.VarChar).Value = TextBox2.Text ;
cmd.Parameters.Add(@ dept,SqlDbType.VarChar).Value = TextBox3.Text;
cmd.Parameters.Add(@ DOB,SqlDbType.SmallDateTime).Value = TextBox4.Text;
cmd.Parameters.Add(@ Sex,SqlDbType.VarChar).Value = RadioButton1.SelectedItem.Text;
cmd.Parameters.Add(@ Address,SqlDbType.VarChar).Value = TextBox5.Text;
string sa = CreateRandomPassword(6);
cmd.Parameters.Add(@ pass,SqlDbType.VarChar).Value = sa;
con.Open();
cmd.ExecuteNonQuery();
Response.Write(< script> alert('注册成功......!')< / script>);
Response.Write(< script> alert('Password is+ Server.HtmlEncode(sa.ToString())+')< / script>);

}
con.Close();
}
公共静态字符串CreateRandomPassword(int PasswordLength)
{
string allowdChars =0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ;
Random randNum = new Random();
char [] chars = new char [PasswordLength];
int allowedCharCount = allowdChars.Length;
for(int j = 0; j< PasswordLength; j ++)
{
chars [j] = allowdChars [(int)((allowdChars.Length)* randNum.NextDouble()) ]。
}
返回新字符串(字符);
}
}

解决方案

检查括号!

更改:

 使用(SqlCommand cnd =  new  SqlCommand ( 从Emplo选择最后的EmpID),con)

对此:

 使用(SqlCommand cnd =  new  SqlCommand( 从Emplo中选择最后的EmpID,con))


Hello i'm new to asp.net and i'm doing this project for my academics. please help

P.S: currently i'm getting an error and it is "you must provide an initializer in a fixed or using statement declaration"

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=JIMMY-PC;initial Catalog=jimmy;Integrated Security=true");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("home.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        using (SqlCommand cnd = new SqlCommand("select LAST EmpID from Emplo"),con) //Error here
        {
            //How to store the selected field?
        }
        using (SqlCommand cmd = new SqlCommand("Insert into Emplo ([Name],[Password],[Designation],[Dept],[D O B],[Sex],[Address]) values (@Name,@pass,@des,@dept,@DOB,@Sex,@Address)", con))
        {
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = TextBox1.Text;
            //Here i need to add the selected number from the previous statement and insert here to the current row
            cmd.Parameters.Add("@des", SqlDbType.VarChar).Value = TextBox2.Text;
            cmd.Parameters.Add("@dept", SqlDbType.VarChar).Value = TextBox3.Text;
            cmd.Parameters.Add("@DOB", SqlDbType.SmallDateTime).Value = TextBox4.Text;
            cmd.Parameters.Add("@Sex", SqlDbType.VarChar).Value = RadioButton1.SelectedItem.Text;
            cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = TextBox5.Text;
            string sa = CreateRandomPassword(6);
            cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = sa;
            con.Open();
            cmd.ExecuteNonQuery();
            Response.Write("<script>alert('Registered successfully......!')</script>");
            Response.Write("<script>alert('Password is  " + Server.HtmlEncode(sa.ToString()) + "')</script>");
            
        }
        con.Close();
    }
    public static string CreateRandomPassword(int PasswordLength)
        { 
        string allowdChars = "0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
        Random randNum = new Random();
        char[] chars = new char[PasswordLength];
        int allowedCharCount = allowdChars.Length;
        for (int j = 0; j < PasswordLength; j++)
        {
            chars[j] = allowdChars[(int)((allowdChars.Length) * randNum.NextDouble())];
        }
         return new string(chars);       
        }
    }

解决方案

Check your brackets!
Change this:

using (SqlCommand cnd = new SqlCommand("select LAST EmpID from Emplo"),con)

To this:

using (SqlCommand cnd = new SqlCommand("select LAST EmpID from Emplo",con))


这篇关于select和insert语句C#asp.net中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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