Winform C# में कनेक्शन स्ट्रिंग को इकाई में कैसे बदल सकते हैं और Winform कोड के अंदर कनेक्शन कैसे बदल सकते हैं


     `i have this code how can change the connection sql to Entity like NorthwindEntity`

```
> using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Drawing;
   using System.Data;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;
   using System.Windows.Forms;

   namespace Attendance_Management_System.PAL.User_Control
{
    public partial class UserControlAttendance : UserControl
    {
        private string sql = @"Data Source = .\SQLEXPRESS;
                            Initial Catalog = Attendance_Management_System;
                            Integrated Security = True;";
        private bool okay;

        public UserControlAttendance()
        {
            InitializeComponent();
            dataGridViewMarkAttendance.Columns["Column1"].Visible = false;
            dataGridViewMarkAttendance.Columns["Column5"].Visible = false;
        }

        private void comboBoxClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(Attendance.Attendance.IsMarkAttendance(dateTimePickerDate.Text, comboBoxClass.SelectedItem.ToString(), sql))
            {
                Attendance.Attendance.DisplayAndSearchAllData("SELECT Student_Table.Student_ID, Student_Name, Student_Reg, Attendance_Status FROM Student_Table INNER JOIN Attendance_Table ON Student_Table.Student_ID = Attendance_Table.Student_ID INNER JOIN Class_Table ON Class_Table.Class_ID = Student_Table.Class_ID WHERE Attendance_Date = '" + dateTimePickerDate.Text + "' AND Class_Name = '" + comboBoxClass.SelectedItem.ToString() + "';", dataGridViewMarkAttendance, sql);
                okay = true;
            }
            else
            {
                Attendance.Attendance.DisplayAndSearchAllData("SELECT Student_ID, Student_Name, Student_Reg FROM Student_Table INNER JOIN Class_Table ON Class_Table.Class_ID = Student_Table.Class_ID WHERE Class_Name = '" + comboBoxClass.SelectedItem.ToString() + "';", dataGridViewMarkAttendance, sql);
                okay = false;
            }
        }

        private void tabPageMarkAttendance_Leave(object sender, EventArgs e)
        {
            if (comboBoxClass.SelectedIndex != -1)
            {
                if (comboBoxClass.SelectedIndex != -1)
                {
                    string status;
                    if (Attendance.Attendance.IsMarkAttendance(dateTimePickerDate.Text, comboBoxClass.SelectedItem.ToString(), sql))
                    {
                        foreach (DataGridViewRow row in dataGridViewMarkAttendance.Rows)
                        {
                            if (Convert.ToBoolean(row.Cells["Column4"].EditedFormattedValue) == true)
                                status = "Present";
                            else
                                status = "Absent";
                            Attendance.Attendance.UpdateAttendance(row.Cells["Column1"].Value.ToString(), dateTimePickerDate.Text, status, sql);
                        }
                    }
                    else
                    {
                        foreach (DataGridViewRow row in dataGridViewMarkAttendance.Rows)
                        {
                            if (Convert.ToBoolean(row.Cells["Column4"].EditedFormattedValue) == true)
                                status = "Present";
                            else
                                status = "Absent";
                            Attendance.Attendance.MarkAttendance(row.Cells["Column1"].Value.ToString(), dateTimePickerDate.Text, status, sql);
                        }
                    }
                }
            }
        }

        private void dataGridViewMarkAttendance_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (comboBoxClass.SelectedIndex != -1)
            {
                if (Attendance.Attendance.IsMarkAttendance(dateTimePickerDate.Text, comboBoxClass.SelectedItem.ToString(), sql) && okay)
                {
                    foreach (DataGridViewRow row in dataGridViewMarkAttendance.Rows)
                    {
                        if (row.Cells["Column5"].Value.ToString() == "Present")
                            row.Cells["Column4"].Value = true;
                        else
                            row.Cells["Column4"].Value = false;
                    }
                }
            }
        }

        private void comboBoxClass_Click(object sender, EventArgs e)
        {
            comboBoxClass.Items.Clear();
            Attendance.Attendance.FillComboBox("SELECT DISTINCT(Class_Name) FROM Class_Table;", comboBoxClass, sql);
        }
    }
}
```




     `i try this in combobox it,s working`

```
    using(Attendance_Management_SystemEntities db=new Attendance_Management_SystemEntities())
            {
                db.Configuration.ProxyCreationEnabled = false;
                comboBoxClass.DataSource = db.Class_Table.ToList();
                comboBoxClass.ValueMember = "Class_ID";
                comboBoxClass.DisplayMember = "Class_Name";
            }
```







`but in private void comboBoxClass_SelectedIndexChanged not find way to do it`

What I have tried:

i try this in 
<pre>private void ucAttendance_Load(object sender, EventArgs e)
        {
            using(Attendance_Management_SystemEntities db=new Attendance_Management_SystemEntities())
            {
                db.Configuration.ProxyCreationEnabled = false;
                comboBoxClass.DataSource = db.Class_Table.ToList();
                comboBoxClass.ValueMember = "Class_ID";
                comboBoxClass.DisplayMember = "Class_Name";
            }
        }

ठीक काम करता है लेकिन कॉम्बोबॉक्स में काम नहीं करता db

private void comboBoxClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Attendance.Attendance.IsMarkAttendance(dTPickerAttend.Text, comboBoxClass.SelectedItem.ToString(), ))
            {
                Attendance.Attendance.DisplayAndSearchAllData("SELECT Student_Table.Student_ID, Student_Name, Student_Reg, Attendance_Status FROM Student_Table INNER JOIN Attendance_Table ON Student_Table.Student_ID = Attendance_Table.Student_ID INNER JOIN Class_Table ON Class_Table.Class_ID = Student_Table.Class_ID WHERE Attendance_Date = '" + dTPickerAttend.Text + "' AND Class_Name = '" + comboBoxClass.SelectedItem.ToString() + "';", dataGridViewMarkAttendance, sql);
                //okay = true;
            }
            else
            {
                Attendance.Attendance.DisplayAndSearchAllData("SELECT Student_ID, Student_Name, Student_Reg FROM Student_Table INNER JOIN Class_Table ON Class_Table.Class_ID = Student_Table.Class_ID WHERE Class_Name = '" + comboBoxClass.SelectedItem.ToString() + "';", dataGridViewMarkAttendance, sql);
                //okay = false;
            }
        }

समाधान 1

सी#
private string sql = @"Data Source = .\SQLEXPRESS;
Initial Catalog = Attendance_Management_System;
Integrated Security = True;";

ऐसा कभी न करें: इसे “हार्डकोडिंग” कहा जाता है और इसका मतलब है कि आपको अपने ऐप स्रोत को डेव और प्रोडक्शन के बीच बदलना होगा – जिसका मतलब है कि आप अप्रयुक्त कोड जारी करते हैं।

इसके बजाय ऐसी चीजों को संग्रहीत करने के लिए हमेशा कॉन्फ़िगरेशन फ़ाइल का उपयोग करें: फिर जब आप कोड जारी करते हैं, तो कॉन्फ़िगरेशन फ़ाइल अपरिवर्तित रहती है और उसे “बस काम करना चाहिए”। आप .NET सेटिंग्स फ़ाइल का उपयोग कर सकते हैं, JSON, या XML का उपयोग कर सकते हैं, या अपना खुद का कोड कोड कर सकते हैं – लेकिन मैं जो करता हूं वह यहां दिखाया गया है: इंस्टेंस स्टोरेज – एप्लिकेशन के बीच कॉन्फ़िगरेशन डेटा साझा करने का एक सरल तरीका[^] – यह संभवतः आपके ऐप के लिए थोड़ा अधिक है, लेकिन यह इसे प्रबंधित करने का एक तरीका दिखाता है।

コメント

タイトルとURLをコピーしました