如何导入我的数据库? [英] How to import my database ?

查看:89
本文介绍了如何导入我的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在这里导入数据库.这是代码:

I don''t know how I can import DB here. This is code :

namespace f3m
{
    public partial class sign_in : Form
    {
        string connectionString = f3m.Properties.Settings.Default.Setting;
        SqlConnection con;
        SqlCommand cmd;
        DataSet ds;
        Random rd = new Random();
        // declaration of sql command of userinfo table 
        string selectStringuser = "select * from UserInfo";
        string dataTableNameuser = "UserInfo";
        SqlDataAdapter daUser;
        // declaration of sql command of Session table 
        string insertString = "INSERT INTO Session ( UserId,MultiCastSessionIP,SessionName,TeacherName,Description) VALUES ( @UserId,@MultiCastSessionIP,@SessionName,@TeacherName,@Description) ";
        string selectStringsession = "select * from Session";
        string dataTableNamesession = "Session";
        SqlDataAdapter daSession;
        // Global varables
        string TeacherName;
        string userid;
        public sign_in()
        {
            InitializeComponent();
             InitializeCommand();
             rd.Next(224, 239);
             Form1 obj = new Form1();
             // userid 
             userid = obj.txtBoxUserID.Text;
             // TeacherName
             for (int i = 0; i < ds.Tables[dataTableNamesession].Rows.Count - 1; i++)
             {
                 if (userid == ds.Tables[dataTableNameuser].Rows[i]["UserId"].ToString())
                 {
                     TeacherName = ds.Tables[dataTableNameuser].Rows[i]["NickName"].ToString();
                 }
             }
        }
        void InitializeCommand()
        {
            con = new SqlConnection(connectionString);
            cmd = new SqlCommand();
            try
            {
                con.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            daUser = new SqlDataAdapter(selectStringuser, con);
            daSession = new SqlDataAdapter(selectStringsession, con);
            ds = new DataSet();
            daUser.Fill(ds,dataTableNameuser);
            daSession.Fill(ds,dataTableNamesession);
        }
     // clear all text box of this form 
     void clearField()
        {
            txtSessionName.Text = "";
            txtDescription.Text = "";
            txtSessionFinishTime.Text = "";
            txtSessionStartTime.Text = "";
        
        }
        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void sign_in_Load(object sender, EventArgs e)
        { // display some field of table Session on listbox1
            foreach(DataRow r in ds.Tables[dataTableNamesession].Rows)
            {
                listBox1.Items.Add(r[2]+"\t"+r[3]+"\t"+r[4]+"\t"+r[5]+"\t"+r[6]+"\t"+r[7]);
            }

        }
        // create session button 
        private void button1_Click(object sender, EventArgs e)
        {    //insert
            if (txtDescription.Text == "" || txtSessionName.Text == "")
                MessageBox.Show("First Fill The Blank(s)");
            else
            {

                cmd = con.CreateCommand();
                cmd.CommandText = insertString;
                cmd.Parameters.Add("@UserId", SqlDbType.VarChar, 50);
                cmd.Parameters.Add("@MultiCastSessionIP", SqlDbType.VarChar, 50);
                cmd.Parameters.Add("@SessionName", SqlDbType.Char, 10);
                cmd.Parameters.Add("@TeacherName", SqlDbType.Char, 10);
                cmd.Parameters.Add("@Description", SqlDbType.Text);
               cmd.Parameters.Add("@SessionStartTime", SqlDbType.Time);
                cmd.Parameters.Add("@SessionFinishTime", SqlDbType.Time);
                cmd.Parameters.Add("@Date", SqlDbType.Date);
                cmd.Parameters.Add("@Day", SqlDbType.VarChar, 50);
                //userid
                cmd.Parameters["@UserId"].Value = userid;
                //multicastip
                for (int i = 0; i < ds.Tables[dataTableNamesession].Rows.Count - 1; i++)
                {
                    if (rd != ds.Tables[dataTableNamesession].Rows[i]["MultiCastSessionIP"])
                    {
                        cmd.Parameters["@MultiCastSessionIP"].Value ="192.0.0.1";
                  }
                    else
                    {
                        rd.Next(224, 239);
                    }
                }
                // sessionName,TeacherName,Descrition
                cmd.Parameters["@SessionName"].Value = txtSessionName.Text;
                cmd.Parameters["@TeacherName"].Value = TeacherName;
                cmd.Parameters["@Description"].Value = txtDescription.Text;
               
               // if user fill SessionStartTime,SessionFinishTime,Date,Day.textbox
             if (txtSessionStartTime.Text != "" || txtSessionFinishTime.Text != "" || comboBox1.SelectedItem != "")
                {
                
                cmd.Parameters["@SessionStartTime"].Value = "12.00.00";
                cmd.Parameters["@SessionFinishTime"].Value = "01.00.00";
                    cmd.Parameters["@Date"].Value =  "2/5/2011";
                    cmd.Parameters["@Day"].Value = comboBox1.SelectedItem;

                }
                else
                {  //if user fill SessionStartTime,SessionFinishTime,Date,Day.textbox
                    MessageBox.Show("are u sure to start session now");
                    Session ob= new Session();
                    ob.Show();

                }
                cmd.ExecuteNonQuery();
                MessageBox.Show("Session Done Successfuly", "Congratulation", MessageBoxButtons.OK);
                clearField();

            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

推荐答案

我的意思是因为此代码与数据库有关,所以可能有人需要我的数据库,但实际上我可以.在这里添加它

不,您不需要将数据库放在本地或应用程序文件夹中.

您需要做的是将数据库托管在某个地方并公开,以便您可以使用连接字符串连接到该数据库.
阅读有关连接字符串 [ ^ ].

可以在这里阅读有关数据通信的更多信息:使用ADO.NET访问数据 [ ^ ]
i mean because this code is related to data base so may be any one need my data base but really i can''t add it it here

No, you don''t need to put your database locally or in the application folder or so.

All you need is to have a database hosted somewhere and exposed such that you can connect to it using connection string.
Read about Connection String[^] here.

Further info regarding the data communication can be read here: Accessing data with ADO.NET[^]


希望 [ ^ ]可能会帮助您解决问题.
Hope this[^] might help you to solve your problem.


这篇关于如何导入我的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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