Гость
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Как сделать такую задачу с DataGridView? / 2 сообщений из 2, страница 1 из 1
24.11.2016, 12:23
    #39353528
Ролг Хупин
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Как сделать такую задачу с DataGridView?
Есть две таблицы в SQL Server базе

Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
create table standard_dictionary
(
code int not null primary key,
name nvarchar(256)
)

create table user_dictionary
(
code int not null primary key,
user_code int not null,
new_code int
)




Хотел сделать по-быстрому форму для редактирования одного поля new_code(только апдейта) из user_dictionary в гриде, но чтобы показывались имена из standard_dictionary типа такого

Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
 private SqlConnection con;
        private SqlDataAdapter adap;
        private DataSet ds;
        private SqlCommandBuilder cmdb;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                cmdb=new SqlCommandBuilder(adap);
                adap.Update(ds, "Test");
            }
            catch (Exception x)
            {
                Console.WriteLine(x);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                con = new SqlConnection();
                con.ConnectionString = @"Server=srv\x14;Database=sample;Trusted_Connection=True;";
                con.Open();
                ds = new DataSet();
                adap = new SqlDataAdapter(string.Format(@"select user_dictionary.code, user_dictionary.new_code, name from user_dictionary inner join standard_dictionary on user_dictionary.code=standard_dictionary.code and user_code={0};",uc), con);
                adap.Fill(ds, @"test");
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception x)
            {
                Console.WriteLine(x);
            }
        }




но, естественно, пшет, что динамический sql не поддерживается для двух базовых таблиц.

Как можно решить?
...
Рейтинг: 0 / 0
24.11.2016, 13:52
    #39353633
Cat2
Модератор форума
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Как сделать такую задачу с DataGridView?
Если SqlDataAdapter не нужен, то так
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
// adap = new SqlDataAdapter(string.Format(@"select user_dictionary.code, user_dictionary.new_code,
name from //user_dictionary inner join standard_dictionary on user_dictionary.code=standard_dictionary.code 
and user_code={0};",uc), con);
//                adap.Fill(ds, @"test");


SqlCommand sql = new SqlCommand( string.Format(@"select user_dictionary.code, user_dictionary.new_code, 
name from user_dictionary inner join standard_dictionary on user_dictionary.code=standard_dictionary.code 
and user_code={0};",uc), con);
DataTable dt = new DataTable();
con.Open();
dt.Load(sql.ExecuteReader();
con.Close()

ds.Tables.Add(dt);



Если нужен,

Код: c#
1.
2.
3.
4.
5.
6.
7.
adap = new SqlDataAdapter();

SqlCommand sql = new SqlCommand( string.Format(@"select user_dictionary.code, user_dictionary.new_code, 
name from user_dictionary inner join standard_dictionary on user_dictionary.code=standard_dictionary.code
and user_code={0};",uc), con);

adap.SelectCommand = sql;



Собирать стрку запроса из кусков - дурной тон. Используйте запросы с параметрами.
====
ЗЫ. Синтаксис не проверял
...
Рейтинг: 0 / 0
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Как сделать такую задачу с DataGridView? / 2 сообщений из 2, страница 1 из 1
Целевая тема:
Создать новую тему:
Автор:
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]