SQL如何从两个表连接ID以创建新表(创建新帐户)

时间:2019-12-28 13:56:41

标签: c# sql

我正在尝试使用 在线服务器(仅是原型)在 中为银行应用程序创建数据库

那就是我拥有的


enter image description here


我的连接字符串是:

server=******database.com;user=*****;database=******;password=******;AllowUserVariables=True;

我在蔚蓝上有数据库,并且一切都很好,但是当我在线更改免费数据库后,在尝试添加新用户时遇到了这个问题:


enter image description here


这是我的代码:

 public static void CreateAccount()
        {
            if (CheckIfPersonExist(Client.Pesel))
            {
                MessageBox.Show("This person already has account");
            }
            else { 
                string personTableQ = "INSERT INTO PersonTable VALUES("+"'' ," +
                    "'" + Client.Name + "', '" + Client.Surname + "', " +
                    "'" + Client.City + "', '" + Client.ZipCode + "', " +
                    "'" + Client.Email + "', '" + Client.DateOfBirth.ToString() + "', " +
                    "'" + Client.PhoneNumber + "', '" + Client.Pesel + "')";

                string userTableQ = "INSERT INTO UserTable VALUES(" + "'' ," +
                    "'" + Client.Login + "', '" + Client.Password + "', '" + Client.IsAdmin.ToString() + "')";

                string createAccountQ = "DELIMITER $$" +
                "Begin" +
  Declare @PersonId int" +
                "    Declare @UserId int" +
                "    if (@PersonId is null)" +
                "                Begin" +
                "                    " + personTableQ +
                "                   Select @PersonId = SCOPE_IDENTITY()" +
                "               End" +
                "    if (@UserId is null)" +
                "                Begin" +
                "                    " + userTableQ +
                "                    Select @UserID = SCOPE_IDENTITY()" +
                "               End" +
                "    Insert into AccountTable values("+"'', "+"@PersonId, @UserId, '" + Client.AccountNumber + "', '" + Client.Balance.ToString().Replace(",",".") +"')" +
                "               End" +
                "    Insert into AccountTable values("+"'', "+"@PersonId, @UserId, '" + Client.AccountNumber + "', '" + Client.Balance.ToString().Replace(",",".") +"');" +
                "End$$" +
                "DELIMITER ;";
                DataBaseManager.Post(createAccountQ);
            }
        }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data;

namespace Bank_App.Forms
{
    class DataBaseManager
    {
        private static string connectionString = "server=******database.com;user=*****;database=******;password=******;AllowUserVariables=True;";
        public static string ConnectionString
        {
            get
            {
                return connectionString;
            }
        }

        public static DataTable Get(string query) 
        {
            MySqlConnection sqlcon = new MySqlConnection(connectionString);
            DataTable dataTable = new DataTable();
            try
            {
                sqlcon.Open();
                MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(query, sqlcon);
                sqlDataAdapter.Fill(dataTable);
            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                sqlcon.Close();
            }

            return dataTable;

        }

        public static void Post(string query) {

            MySqlConnection sqlcon = new MySqlConnection(connectionString);
            try
            {
                sqlcon.Open();
                MySqlCommand command = new MySqlCommand(query, sqlcon);
                command.ExecuteNonQuery();

            }
            catch(Exception e) 
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                sqlcon.Close();
            }
        }
    }
}

0 个答案:

没有答案