visual studio做的窗口应用程序,已经连接数据库,如何删除,updata数据

作者&投稿:冻耐 (若有异议请与网页底部的电邮联系)
如何在Visual Studio中让应用程序访问数据库~

在Visual Studio中让应用程序访问数据库的操作方法和步骤如下:
1、首先,打开vs2010并依次单击工具栏的“文件”-->“新建”,新建相应的应用程序,如下图所示。




2、其次,完成上述步骤后,与数据库建立连接,通过“视图”菜单找到“服务器资源管理器”选项,然后单击打开,如下图所示。





3、接着,完成上述步骤后,在服务器资源管理器中右键单击“数据连接”,然后选择“添加连接”选项,如下图所示。





4、然后,完成上述步骤后,选择或输入“服务器名”,选择登录方式,选择或输入数据库名称,如下图所示。





5、最后,完成上述步骤后,通过“视图”菜单打开“属性”选项卡,然后将连接字符串中的那一句直接复制到web.config中,如下图所示。这样,问题就解决了。

public bool ExecuteSQL()
{
string sql="insert into XX values('"+tb_name.text+"','"+tb_age.text+"','"+tb_phone.text+"')";

using (SqlConnection conn = new MySqlConnection("连接数据库语句"))
{
if (conn.State == ConnectionState.Closed)
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = string.Intern(sql);
try
{
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
conn.Close();
throw ex;
}
}
}
}

public class StudentService

    {


//从配置文件中读取数据库连接字符串

        private readonly static string connString = ConfigurationManager.ConnectionStrings["accpConnectionString"].ToString();

        private readonly static string dboOwner = ConfigurationManager.ConnectionStrings["DataBaseOwner"].ToString();

        AdoNetModels.Student model = new Student();

        // const string spName = ".usp_DeleteStudent";


#region  删除数据1

        public int DeleteStudent(int stuID)

        {

            int result = 0;

            // 数据库连接 Connection 对象

            SqlConnection connection = new SqlConnection(connString);

            // 构建删除的sql语句

            string sql = string.Format("Delete From Student  Where stuID={0}", stuID);

            // 定义command对象

            SqlCommand command = new SqlCommand(sql, connection);


            try

            {

                connection.Open();

                result = command.ExecuteNonQuery();  // 执行命令

            }

            catch (Exception ex)

            {


                Console.WriteLine(ex.Message);

            }

            finally

            {

                connection.Close();

            }

            return result;

        }

        #endregion


        #region 删除数据2


public int DeleteStudent2(int stuID)

        {

            int result = 0;

            // 构建删除的sql语句使用参数

            string sql = "Delete From Student  Where stuID=@stuID";


            using (SqlConnection connection = new SqlConnection(connString))

            {

                SqlCommand objCommand = new SqlCommand(sql, connection);


                objCommand.Parameters.Add("@stuID", SqlDbType.Int).Value = stuID;


                connection.Open();

                result = objCommand.ExecuteNonQuery();

            }

            return result;


        }

        #endregion


public int InsertStudent(Student model)

        {


            int result = 0;

            SqlConnection connection = new SqlConnection(connString);

            // 构建插入的sql语句

            string sql = string.Format("INSERT INTO Student (stuName,age) values('{0}','{1}')",

                model.StuName, model.Age);

            // 定义command对象

            SqlCommand command = new SqlCommand(sql, connection);


            try

            {

                connection.Open();

                result = command.ExecuteNonQuery();  // 执行命令


}

            catch (Exception ex)

            {


                Console.WriteLine(ex.Message);

            }

            finally

            {

                connection.Close();

            }


return result;

        }


        public int InsertStudent2(Student model)

        {


            int result = 0;

            // 构建插入的sql语句

            string sql = "INSERT INTO Student (age,stuName) values(@age,@stuName)";

            using (SqlConnection connection = new SqlConnection(connString))

            {

                SqlCommand objCommand = new SqlCommand(sql, connection);


                objCommand.Parameters.Add("@age", SqlDbType.Int).Value = model.Age;

                objCommand.Parameters.Add("@stuName", SqlDbType.NVarChar, 50).Value = model.StuName;


                connection.Open();

                result = objCommand.ExecuteNonQuery();

            }

            return result;

        }


        public int InsertStudent3(Student model)

        {

            int result = 0;

            using (SqlConnection connection = new SqlConnection(connString))

            {

                SqlCommand objCommand = new SqlCommand(dboOwner + ".usp_InsertStudent", connection);

                objCommand.CommandType = CommandType.StoredProcedure;


                objCommand.Parameters.Add("@age", SqlDbType.Int).Value = model.Age;

                objCommand.Parameters.Add("@stuName", SqlDbType.NVarChar, 50).Value = model.StuName;


                connection.Open();

                result = objCommand.ExecuteNonQuery();

            }

            return result;


        }

    

        public int InsertStudent5(Student model)

        {

            int outputResult = 0;

            int returnvalue = 0;

            using (SqlConnection connection = new SqlConnection(connString))

            {

                SqlCommand objCommand = new SqlCommand(dboOwner + ".usp_InsertStudent", connection);

                objCommand.CommandType = CommandType.StoredProcedure;


                objCommand.Parameters.Add("@age", SqlDbType.Int).Value =  model.Age;

                objCommand.Parameters.Add("@stuName", SqlDbType.NVarChar, 50).Value = model.StuName;

                //定义输出参数

                SqlParameter parameter = new SqlParameter("@stuID", SqlDbType.Int);

                parameter.Direction = ParameterDirection.Output;

                objCommand.Parameters.Add(parameter);


                //定义ReturnValue参数

                objCommand.Parameters.Add("ReturnValue", SqlDbType.Int);

                objCommand.Parameters["ReturnValue"].Direction = ParameterDirection.ReturnValue;

                connection.Open();

                objCommand.ExecuteNonQuery();// 执行命令

                //获取输出参数的值在命令执行以后

                outputResult = (int)objCommand.Parameters["@stuID"].Value;

                //存储过程中为定义return值默认为0

                returnvalue = (int)objCommand.Parameters["ReturnValue"].Value;

            }

            return returnvalue;


        }


public IList<Student> GetAllStudents1()

        {


            IList<Student> dataList = new List<Student>();

            DataSet dataSet = new DataSet();  // 声明并初始化DataSet

            SqlDataAdapter dataAdapter;       // 声明DataAdapter


            using (SqlConnection conn = new SqlConnection(connString))

            {

                // 定义command对象

                SqlCommand command = new SqlCommand(dboOwner + ".usp_SelectStudentsAll", conn);

                command.CommandType = CommandType.StoredProcedure;

                //Command定义带参数的SQL语句的参数

                //command.Parameters.Add("@stuID", SqlDbType.Int);

                //给输入参数赋值

                //command.Parameters["@stuID"].Value = 5; 

                conn.Open();

                // 初始化 DataAdapter

                dataAdapter = new SqlDataAdapter(command);


                // 填充 DataSet

                dataAdapter.Fill(dataSet, "dataSetName");


                // 处理数据集中的数据                   

                foreach (DataRow row in dataSet.Tables[0].Rows)

                {

                    Student model = new Student();

                    model.StuId = Convert.ToInt32(row["stuID"]);

                    model.StuName = Convert.ToString(row["stuName"]);

                    dataList.Add(model);


                }


            }

            return dataList;


}


        public void MoreResult()

        {

            DataSet dataSet = new DataSet();  // 声明并初始化DataSet

            SqlDataAdapter dataAdapter;       // 声明DataAdapter


            // 定义查询语句

            string sql = string.Format("SELECT * FROM student where stuid>50;SELECT * FROM student ");

            SqlConnection connection = new SqlConnection(connString);

            try

            {

                connection.Open();

                // 初始化 DataAdapter

                dataAdapter = new SqlDataAdapter(sql, connection);


                // 填充 DataSet

                dataAdapter.Fill(dataSet, "dataSetName");


                // 处理数据集中的数据

                foreach (DataRow row in dataSet.Tables[0].Rows)

                {

                    //int gradeId = (int)row["GradeID"];


                }

            }

            catch (Exception ex)

            {


                Console.WriteLine(ex.Message);

            }

            finally

            {

                connection.Close();

            }


        }


}

}




南充市17161189648: 如何用Visual Studio 2012创建窗口程序 -
线杨复方: 1、打开VS20122、左上角file->new->project3、在弹出选择工程的地方选择 WindowsFormsApplication4、点击右下角OK,就可以了

南充市17161189648: visual studio打开一个新窗体 -
线杨复方: 首先要有2个窗体Form1和Form2; Form1代码: protected void label1_Click() // label的点击事件 { Form2 frm = new Form2(); frm.Show(); }

南充市17161189648: 在Visual studio中,我做了两个窗体.一个是登录窗体,一个是主界面窗体,当我登录后, -
线杨复方: 在程序的起点,默认为program.cs里添加代码: 登陆 log = new 登陆(); log.ShowDialog(); if (log.DialogResult == DialogResult.OK) { Application.Run(new 主面板(log.UserID)); } 在登录窗口中,如果验证成功,则执行如下代码:this.DialogResult = DialogResult.OK; return; 这样应该就可以满足你的要求了.

南充市17161189648: 怎样用Visual Studio制作小型计算器 -
线杨复方: 打开Visual Studio工具,新建一个window应用窗体.确定后出现了我们第一个窗体Form1.我们就在这制作小型计算器,出现的界面如下: 修改下form1的名称.右键上面的窗体打开属性,找到Text属性,把form1改为:小型计算器拖入控...

南充市17161189648: 如何使用visual studio 2013 创建一个简易窗口
线杨复方: 不同的语言是不一样的,如果用C#的话,你只需要新建项目,选择windows床体项目,完成,一行代码都不用谢就创建了一个简易的窗口.

南充市17161189648: Microsoft Visual Studio 2005绘图窗口怎么生成? -
线杨复方: 建一个工程 选择windows窗体就行了

南充市17161189648: visual studio做一个菜单模式的可视化窗口应用程序
线杨复方: 新建一个windows的窗体应用程序就可以啦!

南充市17161189648: 请问Visual studio里面的自动窗口,局部变量,监视和即时窗口都是干什么用的呢?怎么觉得都差不多? -
线杨复方: 自动窗口 自动根据当前选中对象显示其调试信息 局部变量 只显示当前帧的局部变量信息 监视窗口 用户自己添加的变量信息 即时窗口 用户可在次输入语句以执行,相当命令行

南充市17161189648: 如何使用visual studio 2010 创建一个对话框程序 -
线杨复方: 打开VisualStudio2010,选择“文件”——“新建”——“项目”菜单命令.调出“新建项目”对话框.在“新建项目”对话框右侧的列表中选择“控制台应用程序”选项,在“名称"中输入项目名称,设置”位置“,其余默认,最后单击”确定“.在VisualStudio2010的资源管理器中双击Program.cs,打开Program.cs文件,编写代码.4 选择"调试“——”启动调试“菜单命令,启动调试过程.或者直接按”F5“键.5 若无错误,则弹出程序运行结果窗口.

南充市17161189648: 怎样使用visual studio 2010以C语言编一个简单的窗口程序 -
线杨复方: VC2010 生成的工程默认是 Unicode 编码,字符串必须是 Unicode 字符,在前面加上 L:MessageBox(NULL, L"Hello World!", L"我的第一个窗口程序", MB_OK); 一般来说,用 VC 开发程序都不这样写,而是应该是使用微软定义的数据...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网