当前位置:编程学习 > C#/ASP.NET >>

C# 输入数据从FORM到数据库

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FrmProfile : Form
    {
        public FrmProfile()  
        {
            System.Data.SqlClient.SqlConnection con;          
            InitializeComponent();
        }
        private void FrmProfile_Load(object sender, EventArgs e)
        {
            con = new System.Data.SqlClient.SqlConnection();

            con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Basketball Sys_Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            con.Open();

            MessageBox.Show("Database Open");

            con.Close();

            MessageBox.Show("Database Closed"); 
        }

        private void btnProfileOK_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Saved");
            DialogResult = DialogResult.OK; 
        }

        private void btnProfileNext_Click(object sender, EventArgs e)
        {
            new FrmTeamInfo().ShowDialog(); 
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

      
    }
}

请帮我看看这段代码,主要是那con底下总是有个红色下划线,有错误.系统显示:"Error 4 The name 'con' does not exist in the current context D:\2010 Project 300\WindowsFormsApplication2\WindowsFormsApplication2\FrmProfile.cs 25 13 WindowsFormsApplication2
" --------------------编程问答-------------------- con = new System.Data.SqlClient.SqlConnection();
这是赋值
没定义呀 --------------------编程问答-------------------- public FrmProfile() 
{
System.Data.SqlClient.SqlConnection con; 
InitializeComponent();
}
这里面定义也只是个局部变量
呆在这个函数里有用 --------------------编程问答-------------------- 把
System.Data.SqlClient.SqlConnection con; 
放到form load里去呗  或者直接定义在namespace下
--------------------编程问答-------------------- 嗯,你是对的,谢啦! --------------------编程问答-------------------- lz,你剛剛開始學WinForm吧?? --------------------编程问答-------------------- 嗯,我刚开始学C#,有什么高招和好建议么? --------------------编程问答-------------------- System.Data.SqlClient.SqlConnection con;
这个变量要声明为类级别的才可以在其他方法中调用,或者是在方法内声明
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,