Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中将组合框字符串值转换为int,并在SQL server DB中传递值_C#_Sql_Sql Server_Sql Server 2014 - Fatal编程技术网

C# 在C中将组合框字符串值转换为int,并在SQL server DB中传递值

C# 在C中将组合框字符串值转换为int,并在SQL server DB中传递值,c#,sql,sql-server,sql-server-2014,C#,Sql,Sql Server,Sql Server 2014,我希望我能清楚地解释我在编程方面的困境。我是编程新手,所以请容忍我。我正在使用C和SQL Server 2014 我在SQL server中创建了一个数据库,其中包含名为Roles和AccountRegistration的表: 角色表中有RoleIdpk和RoleName,而 AccountRegistration表包含Usernamepk、Password、RoleIdfk、FullName和Address 这两个表都通过RoleID进行关联 现在,我创建了一个帐户注册表C,其中包含用户名、密

我希望我能清楚地解释我在编程方面的困境。我是编程新手,所以请容忍我。我正在使用C和SQL Server 2014

我在SQL server中创建了一个数据库,其中包含名为Roles和AccountRegistration的表:

角色表中有RoleIdpk和RoleName,而 AccountRegistration表包含Usernamepk、Password、RoleIdfk、FullName和Address

这两个表都通过RoleID进行关联

现在,我创建了一个帐户注册表C,其中包含用户名、密码、全名、地址和角色名

RoleName位于组合框中,如果用户选择Admin或Employee,如何将其转换为int,以便将其传递到RoleId字段

下面是我尝试过的仅使用SQL server的方法,但它不起作用

INSERT INTO AccountRegistration(RoleID)
SELECT a.RoleName
FROM Roles a
INNER JOIN AccountRegistration b ON cast(a.RoleId as nvarchar(255))=b.RoleID
错误消息:

Msg 245,16级,状态1,第1行

将nvarchar值“Admin”转换为数据类型int时,转换失败

我还没有尝试过用C编写代码,因为我仍在尝试找出逻辑

SqlConnection con = new SqlConnection(@"Data Source=***\sqlexpress;Initial Catalog=****;User ID=**;Password=****");
       

        public FrmAddNewAccount()
        {
            InitializeComponent();
        }

        private void FrmAddNewAccount_Load(object sender, EventArgs e)
        {
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("Select * From Roles", con);
            DataSet dataSet = new DataSet();
            sqlDataAdapter.Fill(dataSet);
            cmbRole.DisplayMember = "RoleName";
            cmbRole.ValueMember = "RoleId";
            cmbRole.DataSource = dataSet;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter("Insert into AccountRegistration(Username, Password, RoleID, FullName, Address) VALUES ('" + txtUsername.Text + "', '" + txtPassword.Text + "', '" + cmbRole.Text + "', '" + txtFullName.Text +"', '"+txtAddress.Text+"')", con);
            sda.SelectCommand.ExecuteNonQuery();
            con.Close();
        }

       
        private void cmbRole_SelectedValueChanged(object sender, EventArgs e)
        {
            string RoleId = cmbRole.SelectedValue.ToString();

        }

这确实是一个SQL问题。它不起作用的原因是因为“Admin”不是一个数字。在C语言中,您需要做的事情如下:我将让您编写从SQL Server读取的实际代码:

string[] positionTypes = new String { "admin", "manager", "peon" }; //fill the array with your positions.
public static int ConvertFromPosition(string pos)
{
       return positionTypes.IndexOf(pos.ToLower().Trim());
}
你提供给我们的信息太快了。如果需要与这些索引不相等的特定数值,则需要一个枚举来容纳这些值,并需要一个switch语句来运行比较

public enum MyEnum
        {
            Admin = 10,
            Manager = 20,
            Peon = 30
        }

public static int ConvertFromPosition(string pos)
{
       switch(pos.ToLower().Trim())
         {
           case "admin": return MyEnum.Admin; break;
           case "manager": return MyEnum.Manager; break;
           case "peon": return MyEnum.Peon; break;
           default:
                Throw new ApplicationException("Position " + pos + " is not a valid position.");
         }

}

请注意,有更安全的方法来比较字符串,就像我说的,这只是快速和肮脏的。如果我们是文化特定的,情况会变得更复杂

必须将RoleId和RoleName绑定到组合框

 SqlConnection sqlConnection=new SqlConnection("Your Sql Server Connection");
            SqlDataAdapter sqlDataAdapter=new SqlDataAdapter("Select * From Roles",sqlConnection);
            DataSet dataSet = new DataSet();
            sqlDataAdapter.Fill(dataSet);
            comboBoxRoles.DisplayMember = "RoleName";
            comboBoxRoles.ValueMember = "RoleId";
            comboBoxRoles.DataSource = dataSet;
当在ComboBox中选择了项时,必须将selectedValue作为RoleId发送到t-sql以在表中插入

 private void comboBoxRoles_SelectedValueChanged(object sender, EventArgs e)
    {
        string RoldId = comboBoxRoles.SelectedValue.ToString();
        //RoleId = 35 for example
    }

您的Insert查询应该具有

cmbRole.SelectedValue.ToString而不是cmbRole.Text

SqlDataAdapter sda = new SqlDataAdapter("Insert into AccountRegistration(
Username, Password, RoleID, FullName, Address) VALUES 
('" + txtUsername.Text + "', '" + txtPassword.Text + "', '" + 
cmbRole.SelectedValue.ToString() + "', '" + txtFullName.Text +"', 
'"+txtAddress.Text+"')", con);

这应该可以解决您的问题。

只需将此插入更改为AccountRegistrationRoleID SELECT a.RoleName,将此插入更改为AccountRegistrationRoleID SELECT a.RoleId谢谢@mohsen,但我仍然收到一个错误,您知道我哪里出错了吗?我想这是因为“+CMB角色.文本+”我用代码编辑了我的帖子。错误来自sda.SelectCommand.ExecuteOnQuery;仍然出现错误,表示无法将nvarchar转换为int。在AccountRegistration的插入查询中,您应该传递RoleId,这是从comboBoxRoles.SelectedValue获得的。通过RoleName并不能解决你的问题。而且您的插入查询应该是AccountRegistration表的简单插入查询,而不需要对角色表进行任何操作。这对我很有帮助。谢谢